mysqli_stmt_store_result()
transfers all the rows of the result set into PHP memory. Subsequently "fetching" rows is just iterating over the in-memory result set. This is fine unless your result set is too large; it might exceed your PHP memory limit. This function returns only TRUE or FALSE. You can call it from your mysqli object or from a statement object.
mysqli_use_result()
does not transfer the whole result set. It makes a separate call to the MySQL server each time you fetch a row. This way your PHP memory doesn't have to hold the entire result set at once. You can process each row and discard it. This is useful when the result set is large. This function returns a mysqli_result
resource. You can call it only from the mysqli object, not from a statement object, so it can't be used for prepared statements.
Note that you cannot get the number of rows in a result set before the client (PHP) has fetched all the rows. So trying to read mysqli_stmt_num_rows()
before fetching returns 0, even though you know the result should have something in it. Using the store-result functions counts as fetching the result set for this purpose.
mysqli_stmt_get_result()
is a newer method in the mysqlnd driver. This function returns a mysqli_result
resource from a prepared statement. This helps make it easier to code your fetch loop without using mysqli_stmt_bind_result()
, which some people find confusing.
mysqli_stmt_get_result()
always calls store-result internally, so you better have enough memory in PHP to hold the full result set. Given this, I don't know of any way to do row-by-row fetching if you use a prepared statement. You just have to make sure you don't run SQL queries that have large result sets.
Honestly, I avoid mysqli when I work with PHP. I prefer PDO. Lots of usage is cleaner and less confusing. I wish PHP had just deprecated mysqli at the same time that they deprecated mysql. Or at least made a clear statement that PDO was preferred.