1、和mysql擴展庫的區(qū)別:
(1 安全性、穩(wěn)定性更高
(2 提供了面向?qū)ο蠛兔嫦蜻^程兩種風格
2、php.ini 中的 extension=php_mysqli.dll 解除封印
3、面向?qū)ο螅翰樵兞斜?/p>
<?php //mysqli ***作數(shù)據(jù)(面向?qū)ο箫L格) #1、創(chuàng)建Mysql對象 $mysqli=new MySQLi("127.0.0.1","root","daomul","test"); if(!$mysqli) { die("連接失敗!".$mysqli->connect_error); } #2、***作數(shù)據(jù)庫 $sql="select * from user1"; $res=$mysqli->query($sql); #3、處理結(jié)果 while($row=$res->fetch_row()) { foreach($row as $key=> $val) { echo "-- $val"; } echo "<br/>"; } #4、關(guān)閉資源 $res->free();//釋放內(nèi)存 $mysqli->close();//關(guān)閉連接 ?>
4、面向?qū)ο螅悍庋b類后實現(xiàn)
4.1 Sqliconnect.class.php
<?php Class Sqliconnect { private $mysqli; private static $host="127.0.0.1"; private static $root="root"; private static $password="daomul"; private static $db="test"; function __construct() { $this->mysqli=new MySQLi(self::$host,self::$root,self::$password,self::$db); if(!$this->mysqli) { die("數(shù)據(jù)庫連接失??!".$this->mysqli->connect_error); } $this->mysqli->query("set names utf8"); } //查詢***作 public function excute_dql($sql) { $res=$this->mysqli->query($sql) or die("數(shù)據(jù)查詢失敗".$this->mysqli->error); return $res; } //增刪改***作 public function excute_dml($sql) { $res=$this->mysqli->query($sql) or die("數(shù)據(jù)***作失敗".$this->mysqli->error); if(!$res) { echo "數(shù)據(jù)***作失敗"; } else { if($this->mysqli->affected_rows>0) { echo "***作成功!"; } else { echo "0行數(shù)據(jù)受影響!"; } } } } ?>
4.2 調(diào)用頁面startsqli.php
<?php //mysqli ***作數(shù)據(jù)(面向?qū)ο箫L格) require_once "Sqliconnect.class.php"; $Sqliconnect=new Sqliconnect(); //$sql="insert into user1(name,password,email,age) values('帝都',md5('gg'),'sd@sohu.com',23)"; //$sql="delete from user1 where id=11"; //$res=$Sqliconnect->excute_dml($sql); $sql="select name from user1;"; $res=$Sqliconnect->excute_dql($sql); while($row=$) $res->free(); ?>
5、同時執(zhí)行多條數(shù)據(jù)庫語句 multiQuery.php
<?php //mysqli ***作數(shù)據(jù)(面向?qū)ο箫L格) #1、創(chuàng)建Mysql對象 $mysqli=new MySQLi("127.0.0.1","root","daomul","test"); if(!$mysqli) { die("連接失??!".$mysqli->connect_error); } #2、***作數(shù)據(jù)庫 $sqls="select * from user1;"; $sqls.="select * from user2"; #3、處理結(jié)果 if($res=$mysqli->multi_query($sqls)) { echo "211"; do { //從mysqli連續(xù)取出第一個結(jié)果集 $result=$mysqli->store_result(); //顯示mysqli result對象 while($row=$result->fetch_row()) { foreach($row as $key=> $val) { echo "-- $val"; } echo "<br/>"; } $result->free();//及時釋放當前結(jié)果集,并進入下一結(jié)果集 //判斷是否有下一個結(jié)果集 if(!$mysqli->more_results()) { break; } echo "<br/>************新的結(jié)果集**************"; }while($mysqli->next_result()); } #4、關(guān)閉資源 $mysqli->close();//關(guān)閉連接 ?>
6、事務控制
<?php //mysqli ***作數(shù)據(jù)(面向?qū)ο箫L格) // 數(shù)據(jù)庫 :create table account(id int primary key,balance float); $mysqli=new MySQLi("127.0.0.1","root","daomul","test"); if(!$mysqli) { die("數(shù)據(jù)庫連接失敗!".$mysqli->connect_error); } //將提交設(shè)為false $mysqli->autocommit(false); $sql1="update account set balance=balance+1 where id=1;";//沒錯的語句 $sql2="update accounterror2 set balance=balance-1 where id=2";//有錯的語句 $res1=$mysqli->query($sql1); $res2=$mysqli->query($sql2); if(!$res1||!$res2) { //回滾:其中一個不成功即回滾不提交 echo "有錯,回滾,請重新提交!"; $mysqli->rollback();//die("***作失??!".$mysqli->error); } else { //所有均成功則提交 echo "所有提交成功!"; $mysqli->commit(); } $mysqli->close(); /* 1、 start transaction; 開啟事務 2、svaepoint a; 做保存點 3、執(zhí)行***作1; 4、 svaepoint b; 5、執(zhí)行***作2; ... 6、rollback to a/b; 回滾或者是提交 7、commit 事務控制特點acid 原子性/一致性/隔離性/持久性 */ ?>
7、預處理技術(shù)
主要在連接和編譯過程精簡,還可以SQL防止注入
7.1 預編譯插入多個數(shù)據(jù)
<?php //mysqli 預編譯演示 #1、創(chuàng)建mysqli對象 $mysqli=new MySQLi("127.0.0.1","root","daomul","test"); if(!$mysqli) { die("數(shù)據(jù)庫連接失?。?quot;.$mysqli->connect_error); } #2、創(chuàng)建預編譯對象 $sql="insert into user1(name,password,email,age) values(?,?,?,?);";//暫時不賦值,用問號代替 $stmt=$mysqli->prepare($sql) or die($mysqli->error); /********************************可重復執(zhí)行時需要的代碼start*********************************/ #3、綁定參數(shù) $name='小明5'; $password='34f'; $email='ssd@qq.com'; $age='1'; #4、參數(shù)賦值(第一個參數(shù)指代參數(shù)的類型縮寫,string-s,int-i,double-d,bool-b $stmt->bind_param("sssi",$name,$password,$email,$age); #5、執(zhí)行代碼(返回布爾類型) $flag=$stmt->execute(); /********************************可重復執(zhí)行時需要的代碼 end************************************/ #6、結(jié)果以及釋放 if(!$flag) { die("***作失敗".$stmt->error); } else { echo "***作成功!"; } $mysqli->close(); ?>
7.2 預編譯查詢多個數(shù)據(jù)
<?php //mysqli 預編譯演示 #1、創(chuàng)建mysqli對象 $mysqli=new MySQLi("127.0.0.1","root","daomul","test"); if(!$mysqli) { die("數(shù)據(jù)庫連接失敗!".$mysqli->connect_error); } /********************************可重復執(zhí)行時需要的代碼 start*******************************/ #2、創(chuàng)建預編譯對象 $sql="select id,name,email from user1 where id>?;";//id,name,email和后面的結(jié)果集bind_result()對應 $stmt=$mysqli->prepare($sql) or die($mysqli->error); #3、綁定參數(shù) $id=5; #4、參數(shù)賦值(第一個參數(shù)指代參數(shù)的類型縮寫,string-s,int-i,double-d,bool-b $stmt->bind_param("i",$id);//綁定參數(shù) $stmt->bind_result($id,$name,$email);//綁定結(jié)果集 #5、執(zhí)行代碼(返回布爾類型) $stmt->execute(); #6、取出結(jié)果集顯示 while($stmt->fetch()) { echo "<br/>$id--$name--$email"; } /********************************可重復執(zhí)行時需要的代碼 end*******************************/ #7、結(jié)果以及釋放 //釋放結(jié)果 $stmt->free_result(); //關(guān)閉預編譯語句 $stmt->close(); //關(guān)閉數(shù)據(jù)庫連接 $mysqli->close(); ?>
8、其他函數(shù)
(1 獲取行數(shù)和列數(shù) num_rows field_count (2 獲取結(jié)果集的一列 :表頭 例如 $result=$mysqli->query(); $result->fetch_field(); (3 取出數(shù)據(jù) $row=$result->fetch_row(); //獲得每一行數(shù)據(jù) 再通過 foreach($row as $val){} 取出每一個數(shù)據(jù)-->
微信掃碼
關(guān)注PHP中文網(wǎng)服務號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號