標籤:dll 記憶體 connect host strong list 結果 引用 database
PHP配置
extension = php_mysqli.dll
如果不啟用,那麼引用mysqli擴充必須使用絕對路徑引用
extension_dir = "ext"
建立和取消連結
$mysqli = new mysqli();//執行個體化mysqli類$mysqli -> connect("localhost","root","123");//連結資料庫$mysqli -> select_db("text");//選擇text資料庫$mysqli -> close();//關閉連結
擷取錯誤資訊
$mysqli = new mysqli("localhost","root","123","test");echo $mysqli -> errno;//沒有錯誤返回0
$mysqli = new mysqli("localhost","root","123");//執行個體化mysqli類$mysqli -> select_db("text");//選擇text資料庫if($mysqli -> errno){echo $mysqli -> error;//Unknown database ‘text‘ 沒有text資料庫}$mysqli -> close();//關閉連結
//mysql.connect.php檔案<?php$mysqli = new mysqli("localhost","root","123","test");?>
<?phpinclude "mysql.connect.php";//調用mysql.connect.php檔案?>
與資料庫互動
·擷取資料
query()方法
mysqli_store_result 較高的記憶體和處理需求,查詢整個結果集(預設)
mysqli_use_result 較低的記憶體需求,查詢幾行結果集
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "select id, name, age from xiu";//建立查詢語句$result = $mysqli -> query($query,MYSQLI_STORE_RESULT);//$query()方法負責將query發送到資料庫while(list($id,$name,$age) = $result -> fetch_row()){//fetch_row()方法將獲得的值產生一個數組 printf("%d*%d*%d",$id,$name,$age);}$mysqli -> close();//關閉資料庫連結
·插入、刪除或更新資料
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "alter table xiu add column birdate date";//建立查詢語句$mysqli -> query($query);//$query()方法負責將query發送到資料庫echo $mysqli -> affected_rows;//提示影響的多少行$mysqli -> close();//關閉資料庫連結
·釋放查詢記憶體
有時可能會擷取一個特別大的結果集,會佔用大量記憶體,使用free()方法釋放佔用的記憶體
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "select id, name, age from xiu";//建立查詢語句$result = $mysqli -> query($query,MYSQLI_STORE_RESULT);//$query()方法負責將query發送到資料庫while(list($id,$name,$age) = $result -> fetch_row()){//fetch_row()方法將獲得的值產生一個數組 printf("%d*%d*%d",$id,$name,$age);}$mysqli -> free();//釋放記憶體
·將結果放在對象中
fetch_object()方法將結果集放入對象中
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "select id, name, age from xiu";//建立查詢語句$result = $mysqli -> query($query);//$query()方法負責將query發送到資料庫while($xiu = $result -> fetch_object()){//fetch_object()方法將結果集放入對象中 $id = $xiu -> id; $name = $xiu -> name; $age = $xiu -> age; printf("id:%d,name:%s,age:%d",$id,$name,$age);}$mysqli -> close();//關閉資料庫連結
·使用索引數組和關聯陣列擷取結果
fetch_array()將結果集放入數組
MYSQLI_ASSOC 將行作為一個關聯陣列返回,鍵由欄位表示,值由欄位內容表示
MYSQLI_NUM 將行作為一個數字索引數組返回,元素的屬性有查詢中的順序決定
MYSQLI_BOTH 將行作為關聯陣列和數組索引數組返回
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "select id, name, age from xiu";//建立查詢語句$result = $mysqli -> query($query);//$query()方法負責將query發送到資料庫while($xiu = $result -> fetch_array(MYSQLI_ASSOC)){//fetch_array()方法將結果集放入數組 $id = $xiu["id"]; $name = $xiu["name"]; $age = $xiu["age"]; printf("id:%d,name:%s,age:%d",$id,$name,$age);}$mysqli -> close();//關閉資料庫連結
·確定返回的行數
num_rows屬性返回查詢了多少行資料
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "select id, name, age from xiu";//建立查詢語句$result = $mysqli -> query($query);//$query()方法負責將query發送到資料庫echo $result -> num_rows;//num_rows屬性查詢返回了多少行資料$mysqli -> close();//關閉資料庫連結
·確定受影響的行數
affected_rows屬性返回受insert、update或delet查詢影響的行數
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "insert into xiu values(4,‘user‘,20)";//建立查詢語句$mysqli -> query($query);//$query()方法負責將query發送到資料庫echo $mysqli -> affected_rows;//affected_rows屬性返回影響的行數$mysqli -> close();//關閉資料庫連結
捆綁參數
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "insert into xiu values(?,?,?)";//建立查詢即相對應的預留位置(?)$stmt = $mysqli -> stmt_init();//建立語句對象$stmt -> prepare($query);//為執行準備語句$stmt -> bind_param("dsd",$id,$name,$age);//綁定參數$id = 5;$name = "4";$age = 3;$stmt -> execute();//執行語句$stmt -> close();//恢複語句資源 $mysqli -> close();//關閉資料庫連結
·捆綁變數
$mysqli = new mysqli("localhost","root","123","test");//連結資料庫伺服器並選擇test資料庫$query = "select id,name,age from xiu";//建立查詢即相對應的預留位置(?)$stmt = $mysqli -> stmt_init();//建立語句對象$stmt -> prepare($query);//為執行準備語句$stmt -> execute();//執行語句$stmt -> bind_result($id,$name,$age);//綁定結果參數while($stmt -> fetch()){//fetch()擷取準備語句結果的每一行 printf("id:%d,name:%s,age:%d",$id,$name,$age);}$stmt -> close();//恢複語句資源 $mysqli -> close();//關閉資料庫連結
PHP與MySQL(15)