What is the difference between bind results and fetch_assoc?
I know binding for a prepared statement is great for security... what is the benefit of binding results?
Binding results seems really complicated than the traditional fetch_assoc()
(I've just recently started changing all my MySQL statements to be prepared.)
Bind result will eventually make your life easier by letting you predefine where data from your queries will go every time you run a given query. With fetch_assoc, you have to dig through an array every time you get something from the database.
What I've gathered from: Performance or security differences between mysqli get_result and bind_result?
There is no security risk with returning strings from a database (with the correct validations depending on what you are returning)
get_result()
makes it easier to return a pre-created array from the statement.
bind_result()
makes it easy to work with the values you wish to work with.
Some programmers like to assign each element of the associative array to a regular variable, so they can use them less verbosely. Using bind_result
does this automatically for you, and you don't have to repeat it each time through the fetch loop.
It's basically just a stylistic choice. The main problem with bind_result
is that you have to make sure to keep the order of the arguments in sync with the select list. This is also true for bind_param (which is why PDO allows :name parameters, to solve this), but the benefits outweigh it.
Essentially, It's all down to what the developer prefers to work with. Variables or seeking an array. they both perform the same task, just using bind_result takes away a line of code to fetch the array type.