Example of how to use bind_result vs get_result
The mysqli_stmt::bind_result() method binds variables to a prepared statement for result storage.
The mysqli_stmt::bind_result() method binds variables to a prepared statement for result storage. The variables must be passed by reference. Here is an example of how to use bind_result():
Example of using bind_result() method in PHP
<?php
$id = 1;
$stmt = $mysqli->prepare("SELECT name, age FROM users WHERE id = ?");
if ($stmt === false) {
die('Prepare failed: ' . $mysqli->error);
}
$stmt->bind_param("i", $id);
$stmt->execute();
if ($stmt->errno) {
die('Execute failed: ' . $stmt->error);
}
$stmt->bind_result($name, $age);
$stmt->fetch();
echo "Name: $name, Age: $age";
$stmt->close();
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
The mysqli_stmt::get_result() method is used to retrieve the result set from a prepared statement. The result set is returned as a mysqli_result object. Here is an example of how to use get_result():
Example of using get_result() method in PHP
<?php
$id = 1;
$stmt = $mysqli->prepare("SELECT name, age FROM users WHERE id = ?");
if ($stmt === false) {
die('Prepare failed: ' . $mysqli->error);
}
$stmt->bind_param("i", $id);
$stmt->execute();
if ($stmt->errno) {
die('Execute failed: ' . $stmt->error);
}
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row['name'] . ", Age: " . $row['age'] . "<br>";
}
$stmt->close();Note that get_result() is available only with the mysqlnd driver. If your PHP installation uses the libmysqlclient library, get_result() will not be available. In that case, you must use bind_result() with fetch(), or switch to mysqli_query() for unprepared statements.