Function name: mysqli_stmt::store_result()
Function Description: This method is used to store the result set in a prepared statement object for subsequent operations.
Applicable version: PHP 5, PHP 7
Syntax: bool mysqli_stmt::store_result()
Return value: Return true if the result set is successfully stored; otherwise return false.
Example:
<?php // 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 准备查询语句$stmt = $mysqli->prepare("SELECT id, name FROM users WHERE age > ?"); // 绑定参数$age = 18; $stmt->bind_param("i", $age); // 执行查询$stmt->execute(); // 存储结果集$result = $stmt->store_result(); if ($result) { // 获取结果集中的数据while ($row = $stmt->fetch()) { echo "ID: " . $row['id'] . ", Name: " . $row['name'] . "<br>"; } } else { echo "存储结果集失败"; } // 关闭语句对象和数据库连接$stmt->close(); $mysqli->close(); ?>
In the above example, we first create a mysqli object to establish a connection to the database. Then, we prepare a SQL query statement with parameters and bind the age parameter to the statement.
Next, we execute the query and use the store_result() method to store the result set in the statement object. If the storage is successful, we loop through the data in the result set through the fetch() method and print it out.
Finally, we closed the statement object and database connection.
Please note that the store_result() method can only be used for SELECT queries, and this method is not required for other types of queries, such as INSERT, UPDATE, or DELETE.