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.
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.
Examples you can find on the respective manual pages.
While pro and cons are quite simple:
Anyway, if your idea is to use either function right in the application code - this idea is wrong. Yet as long as you have them encapsulated in some method to return your data from the query, it doesn't really matter, which one to use, save for the fact that you will need ten times more code to implement bind_result.
The deciding factor for me, is whether I call my query columns using
*
.Using
bind_result()
would be better for this:Using
get_result()
would be better for this:Example 1 for
$query1
usingbind_result()
Example 2 for
$query2
usingget_result()
As you can see you can't use
bind_result
with*
. However,get_result
works for both, butbind_result
is simpler and takes out some of the mess with$row['name']
.bind_result()
Pros:
$row['name']
fetch()
Cons:
*
get_result()
Pros:
fetch_assoc()
Cons:
$row[]
get_result() is now only available in PHP by installing the MySQL native driver (mysqlnd). In some environments, it may not be possible or desirable to install mysqlnd.
Notwithstanding, you can still use mysqli to do 'select *' queries, and get the results with the field names - although it is slightly more complicated than using get_result(), and involves using php's call_user_func_array() function. See example at How to use bind_result() instead of get_result() in php which does a simple 'select *' query, and outputs the results (with the column names) to an HTML table.
I think example 2 will only work like this, because store_result and get_result both get the info from the table.
So remove
And change the order a bit. This is the end result:
Main difference I've noticed is that
bind_result()
gives you error2014
, when you try to code nested $stmt inside other $stmt, that is being fetched (withoutmysqli::store_result()
):Example:
Function used in main code.
Main code.