PHP與MySQL(15)

來源:互聯網
上載者:User

標籤:dll   記憶體   connect   host   strong   list   結果   引用   database   

PHP配置

  • 引用mysqli擴充
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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.