Home > php教程 > php手册 > 【小结】有关mysql扩展库和mysqli扩展库的crud操作封装

【小结】有关mysql扩展库和mysqli扩展库的crud操作封装

WBOY
Release: 2016-05-30 16:59:44
Original
1258 people have browsed it

现阶段php如果要操作mysql数据库 php给我们提供了3套库

1、mysql扩展库   面向过程操作

2、mysqli扩展库  面向对象操作和面向过程操作并存  安全性和效率高于mysql扩展库 

3、PDO扩展库    面向对象操作

今天这篇博文主要要谈谈mysql扩展库和mysqli扩展库 主要是记录了着2套crud操作分装 

以下代码段是关于mysqli扩展库关于crud操作的封装

header("Content-type:text/html;charset=utf-8");
	class Sqlitool{
		private $mysqli;

		private static $host="localhost";
		private static $user="root";
		private static $pwd=123456;
		private static $db="test";

		public function __construct(){
			$this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$db);
			if($this->mysqli->connect_error){
				die("链接失败".$this->mysqli->connect_error);
			}

			//设置访问数据库的字符集
			$this->mysqli->query("set names utf8");
		}
		public function execute_dql($sql){
			$res=$this->mysqli->query($sql) or die("操作失败".$this->mysqli->error);
				return $res;
		}
		public function execute_dml($sql){
			$res=$this->mysqli->query($sql) or die("操作失败".$this->mysqli->error);
			if(!$res){
				return 0;  //操作失败
			}else{
				if($this->mysqli->affected_rows>0){
					return 1;// 表示成功
				}else{
					return 2;// 表示没有行收到影响
				}
			}
		}

	}
Copy after login

  以下代码段是关于mysql扩展库关于crud操作的封装

header("Content-type:text/html;charset=utf-8");<br />class sqltool{
		private $con;
		private $host="localhost";
		private $user="root";
		private $password=123456;
		private $db="test";

		//构造函数 初始化数据库操作链接和设置字符集
		function sqltool(){
			$this->con=mysql_connect($this->host,$this->user,$this->password);

			if(!$this->con){
				die("链接数据库错误".mysql_error());
			}
			mysql_select_db($this->db,$this->con);
			mysql_query("set names utf8");

		}
		//完成查询任务呀
		
		public function exeute_dql($sql){
			$res=mysql_query($sql) or die(mysql_error());
			return $res;
		}
	
		
		//表示insert update delete
		public function exeuct_dml($sql){
			$b=mysql_query($sql,$this->con) or die(mysql_error());
			if(!$b){
				return 0;//失败
			}else{
				if(mysql_affected_rows($this->con)>0){
					return 1;//表示成功
				}
				else{
					return 2;//表示没有行数受影响
				}
			
			}
		}<br />}
Copy after login

  

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template