mysqli_use_result()函數(shù)從上次使用 mysqli_real_query() 執(zhí)行的查詢中初始化結(jié)果集的檢索。
mysqli_use_result()函數(shù)開始從先前執(zhí)行的查詢中檢索結(jié)果集
mysqli_use_result($con)
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個表示與MySQL Server的連接的對象。 |
mysqli_use_result()函數(shù)在出現(xiàn)錯誤的情況下返回結(jié)果對象和布爾值false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_use_result()函數(shù)的用法(面向過程風(fēng)格)-
<?php //建立連接 $con = mysqli_connect("localhost", "root", "password", "test"); //執(zhí)行多個查詢,多個查詢是分號隔開 $query = "SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials"; $res = mysqli_multi_query($con, $query); $count = 0; if ($res) { do { $count = $count+1; mysqli_use_result($con); } while (mysqli_next_result($con)); } print("結(jié)果集數(shù): ".$count); mysqli_close($con); ?>
輸出結(jié)果
結(jié)果集數(shù): 3
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語法為$con->use_result();。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例;
<?php $con = new mysqli("localhost", "root", "password", "test"); //多個查詢 $res = $con->multi_query("SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials"); $count = 0; if ($res) { do { $count = $count+1; $con-> use_result(); } while ($con->next_result()); } print("結(jié)果集數(shù): ".$count); //關(guān)閉連接 $res = $con -> close(); ?>
輸出結(jié)果
結(jié)果集數(shù): 3
以下示例檢索多個查詢的所有結(jié)果集的記錄-
//建立連接 $con = mysqli_connect("localhost", "root", "password", "test"); //執(zhí)行多個查詢 $query = "SELECT * FROM players;SELECT * FROM emp"; $res = mysqli_multi_query($con, $query); if ($res) { do { if ($result = mysqli_use_result($con)) { while ($row = mysqli_fetch_row($result)) { print("Name: ".$row[0]."\n"); print("Age: ".$row[1]."\n"); } mysqli_free_result($result); } if (mysqli_more_results($con)) { print("\n"); } } while (mysqli_use_result($con)); } mysqli_close($con);
輸出結(jié)果
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25 Name: Raju Age: 25 Name: Rahman Age: 30 Name: Ramani Age: 22