I would like to see an example of how to call using bind_result
vs. get_result
and what would be the purpose of using one over the other.
Also the pro and cons of using each.
What is the limitation of using either and is there a difference.
Although both methods work with *
queries, when bind_result()
is used, the columns are usually listed explicitly in the query, so one can consult the list when assigning returned values in bind_result()
, because the order of variables must strictly match the structure of the returned row.
$query1
using bind_result()
$query1 = 'SELECT id, first_name, last_name, username FROM `table` WHERE id = ?'; $id = 5; $stmt = $mysqli->prepare($query1); /* Binds variables to prepared statement i corresponding variable has type integer d corresponding variable has type double s corresponding variable has type string b corresponding variable is a blob and will be sent in packets */ $stmt->bind_param('i',$id); /* execute query */ $stmt->execute(); /* Store the result (to get properties) */ $stmt->store_result(); /* Get the number of rows */ $num_of_rows = $stmt->num_rows; /* Bind the result to variables */ $stmt->bind_result($id, $first_name, $last_name, $username); while ($stmt->fetch()) { echo 'ID: '.$id.'<br>'; echo 'First Name: '.$first_name.'<br>'; echo 'Last Name: '.$last_name.'<br>'; echo 'Username: '.$username.'<br><br>'; }
$query2
using get_result()
$query2 = 'SELECT * FROM `table` WHERE id = ?'; $id = 5; $stmt = $mysqli->prepare($query2); /* Binds variables to prepared statement i corresponding variable has type integer d corresponding variable has type double s corresponding variable has type string b corresponding variable is a blob and will be sent in packets */ $stmt->bind_param('i',$id); /* execute query */ $stmt->execute(); /* Get the result */ $result = $stmt->get_result(); /* Get the number of rows */ $num_of_rows = $result->num_rows; while ($row = $result->fetch_assoc()) { echo 'ID: '.$row['id'].'<br>'; echo 'First Name: '.$row['first_name'].'<br>'; echo 'Last Name: '.$row['last_name'].'<br>'; echo 'Username: '.$row['username'].'<br><br>'; }
Pros:
Cons:
Pros:
fetch_all()
method to return all returned rows at onceCons:
Examples you can find on the respective manual pages, get_result()
and bind_result()
.
While pros and cons are quite simple:
get_result()
is the only sane way to handle resultsIn a modern web application the data is never displayed right off the query. The data has to be collected first and only then output has to be started. Or even if you don't follow the best practices, there are cases when the data has to be returned, not printed right away.
Keeping that in mind let's see how to write a code that returns the selected data as a nested array of associative arrays using both methods.
bind_result()
$query1 = 'SELECT id, first_name, last_name, username FROM `table` WHERE id = ?'; $stmt = $mysqli->prepare($query1); $stmt->bind_param('s',$id); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($id, $first_name, $last_name, $username); $rows = []; while ($stmt->fetch()) { $rows[] = [ 'id' => $id, 'first_name' => $first_name, 'last_name' => $last_name, 'username' => $username, ]; }
and remember to edit this code every time a column is added or removed from the table.
get_result()
$query2 = 'SELECT * FROM `table` WHERE id = ?'; $stmt = $mysqli->prepare($query2); $stmt->bind_param('s', $id); $stmt->execute(); $rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
and this code remains the same when the table structure is changed.
And there's more.
In case you decide to automate the boring routine of preparing/binding/executing into a neat function that would be called like this
$query = 'SELECT * FROM `table` WHERE id = ?'; $rows = prepared_select($query, [$id])->fetch_all(MYSQLI_ASSOC);
with get_result()
it will be quite a plausible task, a matter of just a few lines. But with bind_param()
it will will be a tedious quest.
That's why I call the bind_result()
method "ugly".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With