mysqli_stmt::store_result
PHP 5.0.0 及以上版本
mysqli_stmt::store_result() 函数用于将 MySQL 查询结果存储到客户端内存中。调用此函数之后,可以使用 mysqli_stmt::fetch() 来逐行读取结果集。如果没有调用此函数,结果集是服务器端的游标,无法直接在客户端处理。
该函数不接受任何参数。
成功时返回 true,失败时返回 false。
以下是一个使用 mysqli_stmt::store_result() 函数的示例代码:
<?php // 创建数据库连接 $mysqli = new mysqli("localhost", "username", "password", "database"); <p>// 检查连接<br> if ($mysqli->connect_error) {<br> die("连接失败: " . $mysqli->connect_error);<br> }</p> <p>// 准备 SQL 查询<br> $stmt = $mysqli->prepare("SELECT id, name FROM users WHERE active = ?");<br> $stmt->bind_param("i", $active);</p> <p>// 设置参数并执行查询<br> $active = 1;<br> $stmt->execute();</p> <p>// 存储结果<br> $stmt->store_result();</p> <p>// 获取结果的列数<br> $columns = $stmt->num_columns;</p> <p>// 显示结果<br> echo "查询结果列数: " . $columns . "<br>";</p> <p>// 逐行获取数据<br> $stmt->bind_result($id, $name);<br> while ($stmt->fetch()) {<br> echo "ID: $id, Name: $name<br>";<br> }</p> <p>// 关闭语句和连接<br> $stmt->close();<br> $mysqli->close();<br> ?><br>
此示例展示了如何使用 mysqli_stmt::store_result() 来处理 MySQL 查询结果。首先,创建数据库连接并准备一个 SELECT 查询。然后,使用 bind_param() 函数绑定查询参数,执行查询,并通过 store_result() 将查询结果存储到客户端内存中。接着,使用 bind_result() 将结果列绑定到变量,并使用 fetch() 循环逐行获取数据。最后,关闭语句和连接。