mysqli_stmt::get_result
(mysqli_stmt_get_result) Get the result set from the prepared statement
Function name: mysqli_stmt::get_result()
Function Description: This function is used to obtain the result set object from the prepared statement.
Applicable version: PHP 5.3.0 and above
Syntax: mysqli_stmt::get_result()
Return value: Returns a mysqli_result object containing the result set data obtained from the prepared statement.
Example:
// 创建数据库连接$mysqli = new mysqli("localhost", "username", "password", "database"); // 检查连接是否成功if ($mysqli->connect_errno) { echo "连接数据库失败: " . $mysqli->connect_error; exit(); } // 准备查询语句$query = "SELECT id, name, age FROM users WHERE age >= ?"; // 创建预处理语句对象$stmt = $mysqli->prepare($query); // 绑定参数$age = 18; $stmt->bind_param("i", $age); // 执行预处理语句$stmt->execute(); // 获取结果集对象$result = $stmt->get_result(); // 检查是否有结果if ($result->num_rows > 0) { // 遍历结果集while ($row = $result->fetch_assoc()) { echo "ID: " . $row['id'] . ", Name: " . $row['name'] . ", Age: " . $row['age'] . "<br>"; } } else { echo "没有找到匹配的结果"; } // 关闭结果集和数据库连接$result->close(); $mysqli->close();
Notes: