mysql擴充之mysqli,mysqlmysqli

來源:互聯網
上載者:User

mysql擴充之mysqli,mysqlmysqli

mysqli是最好最快的mysql擴充,唯一的缺陷是只適用於mysql的操作。

首先查看mysql是否擴充mysqli(使用phpinfo()函數輸出查看),一般php5以上的版本都是支援mysqli的。mysqli包含三個比較重要的擴充類:mysqli,mysqli_result,mysqli_stmt

mysqli類的使用:

使用mysqli串連資料庫首先要new mysqli

<pre name="code" class="html"><span style="font-size:14px;">$config = array("host" => "localhost",//主機"user" => "root",//使用者"pwd"  => "root",//密碼"dbname" => "test",//資料庫"charset" => "utf8",//字元集設定);$mysqli = new mysqli( $config["host"],$config["user"],$config["pwd"],$config["dbname"]  );//mysqli需要對具體資料庫進行操作</span>

在此申明一句:若資料庫沒開則伺服器會保持一段較長時間的等待過程,因此,操作過程需保證mysql已經開啟。

<span style="font-size:14px;">if( mysqli_connect_errno() )//使用mysqli_connent_errno()函數判斷是否串連到資料庫{echo "資料庫連結失敗";echo mysqli_connect_error();//用mysqli_connect_error()輸出具體錯誤}</span>
一般使用try{。。throw。。} catch(exception $e)來捕抓錯誤提示。具體的關於php異常處理可以參考本部落格其他文章詳解。函數頭用error_reporting(0)來限制一些錯誤的提示。

在進行資料庫操作時需注意,最好將字元集設定好

<span style="font-size:14px;">$mysqli -> set_charset($config["charset"]);//echo "<br />當前連結字元狀態:".$mysqli->character_set_name();</span>
對資料庫的增刪查改與傳統操作差不多

<span style="font-size:14px;">//$sql = "insert into user(id,name,sex)values('1','irudder','man')";//增加//$sql = "update user set id=2 where name='irudder'";//修改//$sql = "delete from user where id ='2'";//刪除$sql = "select * from user";//尋找</span>
使用$mysqli ->query($sql) 操作sql語句;使用$mysqli->errno 判斷是否有錯誤;使用$mysqli->error顯示錯誤


關於mysqli類的部分擴充知識:

<span style="font-size:14px;">echo "當前的mysql資料庫版本:".$mysqli->get_client_info()."<br />";echo "當前的host資訊:".$mysqli->host_info."<br />";echo "當前的伺服器的資訊:".$mysqli->get_server_info()."<br />";echo "當前的伺服器的版本:".$mysqli->server_version."<br />";echo "當前的sql版本是什麼:".$mysqli->protocol_version."<br />";</span>

musqli_result類的使用:

<span style="font-size:14px;">fetch_array - 擷取關聯陣列,既有欄位又有數字索引(效率相對較低)MYSQLI_NUM,MYSQLI_ASSOC,預設MYSQLI_BOTHfetch_object - 返回一個資料對象,可以用物件導向的調用方式(一般不用,效率中等)fetch_row - 擷取索引數組,效率高fetch_assoc - 擷取欄位數組(鍵符數組,效率高)query- 返回mysqli_result對象#1store_result - 返回mysql_result對象#2multi_query - 執行多條sql語句的操作,成功返回true,錯誤返回false</span>
在使用multi_query進行多條SQL操作時,一旦其中有一條發生錯誤,則後面的語句都不會執行,但不會出現錯誤提示。


關於mysqli的stmt類的使用:

預先處理技術:

用戶端將語句發送至資料庫會進行預先處理,然後才返回處理後的請求。

$stmt = $mysqli  -> prepare($sql);//預先處理資料

$stmt > bind_param('is',$id,$name);//i代表id的類型,s代表name的類型,可增加多個,param綁定指定資料庫欄位,ide,name均可任意名字

<span style="font-size:14px;">$sql = "delete from user where id=?";$stmt = $mysqli->prepare( $sql ); //一個預先處理$stmt -> bind_param( 'i',$id );$id = 13;$mysqli -> close(); //關閉了整個mysqli的擴充,釋放記憶體</span>
$stmt包含一個緩衝區的函數store_result():用戶端將資訊發去資料庫,資料庫處理後會存去緩衝區有則直接用,無則更新,用戶端實際每次都是接收緩衝區的處理後資訊,這樣就加快了處理速度。
<span style="font-size:14px;">if( $stmt -> store_result() ) //如果緩衝區裡面有我的資料,那麼我就拿回來{$stmt -> bind_result( $id,$a,$b );while( $stmt->fetch() ){echo "{$id}-----{$a}----{$b}<br />";}}//echo "影響了".$stmt->affected_rows."行記錄!";</span>
<span style="font-size:14px;">........</span>
<span style="font-size:14px;">......</span>
<span style="font-size:14px;">$stmt -> free_result();//釋放掉整個結果集合,緩衝區裡面的資料$mysqli -> close(); //關閉了整個mysqli的擴充</span>
對資料庫記憶體的釋放很重要!








相關文章

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.