mysqli_next_result()函數(shù)為 mysqli_multi_query() 準備下一個結(jié)果集。
mysqli_multi_query() 函數(shù)執(zhí)行之后, 為讀取下一個結(jié)果集做準備, 然后可以使用 mysqli_store_result() 或 mysqli_use_result() 函數(shù)讀取下一個結(jié)果集。
mysqli_next_result($con)
序號 | 參數(shù)及說明 |
---|---|
1 | con(必需) 這是一個表示與MySQL Server的連接的對象。 |
如果有更多的結(jié)果集,mysqli_next_result()函數(shù)將返回true;如果沒有更多的結(jié)果集,或者如果下一個查詢有錯誤,則返回false。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_next_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ū)ο蟮臉邮街?,此函?shù)的語法為$con-> next_result();。以下是面向?qū)ο髽邮胶瘮?shù)的示例;
<?php $con = new mysqli("localhost", "root", "password", "test"); //Multi query $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_next_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