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

mysqli_stmt::bind_result

(mysqli_stmt_bind_result) Preparation statement that binds variables to result store
Name:mysqli_stmt::bind_result
Category:MySQLi
Programming Language:php
One-line Description:Bind the query result to the specified variable so that the query result is obtained after the statement is executed

Function name: mysqli_stmt::bind_result()

Applicable version: PHP 5 >= 5.3.0, PHP 7

Usage: This method is used to bind the query result to the specified variable so that the query result is obtained after the statement is executed.

Syntax: bool mysqli_stmt::bind_result ( mixed &$var1 [, mixed &$... ] )

parameter:

  • &$var1: Required, variable used to store query results. Can be a variable of any type, such as a string, integer, or array. Note that these variables must be allowed to access their values ​​after the statement is executed.
  • &$...: Optionally, multiple variables can be bound, each variable corresponds to a column of the query results.

Return value: Return TRUE when successful, and FALSE when failure.

Example:

connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; exit(); } // Prepare the query statement $query = "SELECT name, age FROM users WHERE id = ?"; // Create preprocessing statement $stmt = $mysqli->prepare($query); // Bind parameter $id = 1; $stmt->bind_param("i", $id); // Execute query $stmt->execute(); // Bind result $stmt->bind_result($name, $age); // Get the result $stmt->fetch(); // Output the result echo "Name: " . $name . "
"; echo "Age: " . $age . "
"; // Close statement and connect $stmt->close(); $mysqli->close(); ?>

In the example above, a database connection is first created. Then, a query statement is prepared and a preprocessing statement is created. Next, the query parameters are bound using the bind_param() method. Then, the query is executed and the query result is bound to the variables $name and $age using the bind_result() method. Finally, use the fetch() method to get the query result and output the result to the browser.

Note that the bind_result() method must be called after the statement is executed, otherwise the result will not be properly bound. In addition, the bound variable must be called before its value can be accessed.

Similar Functions
Popular Articles