mysqli_next_result()函数为 mysqli_multi_query() 准备下一个结果集。
定义和用法
mysqli_multi_query() 函数执行之后, 为读取下一个结果集做准备, 然后可以使用 mysqli_store_result() 或 mysqli_use_result() 函数读取下一个结果集。
语法
mysqli_next_result($con)
参数
序号 | 参数及说明 |
---|---|
1 | con(必需) 这是一个表示与MySQL Server的连接的对象。 |
返回值
如果有更多的结果集,mysqli_next_result()函数将返回true;如果没有更多的结果集,或者如果下一个查询有错误,则返回false。
PHP版本
此函数最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
在线示例
以下示例演示了mysqli_next_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-> next_result();。以下是面向对象样式函数的示例;
<?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("结果集数: ".$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_next_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
PHP mysqli_ping() PHP mysqli_stat()
展开全部