mysqli_use_result()函数从上次使用 mysqli_real_query() 执行的查询中初始化结果集的检索。
mysqli_use_result()函数开始从先前执行的查询中检索结果集
mysqli_use_result($con)
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
mysqli_use_result()函数在出现错误的情况下返回结果对象和布尔值false。
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
以下示例演示了mysqli_use_result()函数的用法(面向过程风格)-
<?php //建立连接 $con = mysqli_connect("localhost", "root", "password", "test"); //执行多个查询,多个查询是分号隔开 $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("结果集数: ".$count); mysqli_close($con); ?>
输出结果
结果集数: 3
在面向对象风格中,此函数的语法为$con->use_result();。以下是面向对象风格中此函数的示例;
<?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("结果集数: ".$count); //关闭连接 $res = $con -> close(); ?>
输出结果
结果集数: 3
以下示例检索多个查询的所有结果集的记录-
//建立连接 $con = mysqli_connect("localhost", "root", "password", "test"); //执行多个查询 $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);
输出结果
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25 Name: Raju Age: 25 Name: Rahman Age: 30 Name: Ramani Age: 22