Current Location: Home> Function Categories> mysqli_stmt::get_result

mysqli_stmt::get_result

(mysqli_stmt_get_result) Get the result set from the prepared statement
Name:mysqli_stmt::get_result
Category:MySQLi
Programming Language:php
One-line Description:Get the result set object 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:

  1. Before using the mysqli_stmt::get_result() function, the mysqli_stmt::execute() function must be executed to execute the preprocessing statement.
  2. This function is only available in PHP versions using mysqlnd driver.
  3. The result set object returned by this function can obtain the result data through fetch_assoc() and other methods.
  4. After the result set object is used, you need to use the close() method to close the result set.
  5. Finally, use the close() method to close the database connection and free up the resources.
Similar Functions
Popular Articles