【小結】有關mysql擴充庫和mysqli擴充庫的crud操作封裝

來源:互聯網
上載者:User

標籤:

現階段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;// 表示沒有行收到影響}}}}

  以下程式碼片段是關於mysql擴充庫關於crud操作的封裝

header("Content-type:text/html;charset=utf-8");
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 deletepublic 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;//表示沒有行數受影響}}}
}

  

【小結】有關mysql擴充庫和mysqli擴充庫的crud操作封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.