The wording above, in the initial description of the function, can be confusing (quoted below).
"You must call mysqli_stmt_store_result() for every query that successfully produces a result set (SELECT, SHOW, DESCRIBE, EXPLAIN), and only if you want to buffer the complete result set by the client, so that the subsequent mysqli_stmt_fetch() call returns buffered data. "
I had initially understood the part saying "and only if you want to buffer..." to mean that it was only necessary to call this function if you wanted to buffer the result set. This, however, is not the case, and the misunderstanding caused me quite a bit of grief.
So, to clarify for anyone suffering from the same misunderstanding, you ALWAYS must call this function for every query that produces a result set (as listed in the parentheses of the quote above), as far as I can tell.
mysqli_stmt::store_result
mysqli_stmt_store_result
(PHP 5)
mysqli_stmt::store_result -- mysqli_stmt_store_result — Transfers a result set from a prepared statement
Descrição
Object oriented style (method):
Procedural style:
You must call mysqli_stmt_store_result() for every query that successfully produces a result set (SELECT, SHOW, DESCRIBE, EXPLAIN), and only if you want to buffer the complete result set by the client, so that the subsequent mysqli_stmt_fetch() call returns buffered data.
Nota: It is unnecessary to call mysqli_stmt_store_result() for other queries, but if you do, it will not harm or cause any notable performance in all cases. You can detect whether the query produced a result set by checking if mysqli_stmt_result_metadata() returns NULL.
Parâmetros
- stmt
-
Apenas para estilo de procedimento: Um identificador de statement retornado por mysqli_stmt_init().
Valor Retornado
Retorna TRUE em caso de sucesso ou FALSE em falhas.
Exemplos
Exemplo #1 Object oriented style
<?php
/* Open a connection */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = $mysqli->prepare($query)) {
/* execute query */
$stmt->execute();
/* store result */
$stmt->store_result();
printf("Number of rows: %d.\n", $stmt->num_rows);
/* free result */
$stmt->free_result();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
Exemplo #2 Procedural style
<?php
/* Open a connection */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = mysqli_prepare($link, $query)) {
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
/* free result */
mysqli_stmt_free_result($stmt);
/* close statement */
mysqli_stmt_close($stmt);
}
/* close connection */
mysqli_close($link);
?>
O exemplo acima irá imprimir:
Number of rows: 20.
Veja Também
- mysqli_prepare() - Prepare a SQL statement for execution
- mysqli_stmt_result_metadata() - Returns result set metadata from a prepared statement
- mysqli_stmt_fetch() - Obtém resultados de um preparado comando e os coloca nas determinadas variáveis
mysqli_stmt::store_result
29-Oct-2008 01:14
27-Dec-2006 09:58
In response to the note below me for the claim that mysqli_fetch_fields is not compatible with prepared statements.
This is untrue, it is but you have to do a little extra work. I would recommend you use a wrapper function of some sort to take care of the dirty business for you but the basic idea is the same.
Let's assume you have a prepared statement like so. I am going to use the procedural way for simplicity but the same idea can be done using the object oriented way:
<?php
// Connect Blah Blah Blah.
$connectionLink = mysqli_connect( .... );
// Query Blab Blah Blah.
$query = "Select `Id` From `Table` Where `Id` = ?";
// Prepare Query.
$prepareObject = mysqli_prepare( $connectionLink , $query );
// Bind Query.
mysqli_stmt_bind_param( $prepareObject , 'i' , 1 );
// Execute Query.
mysqli_stmt_execute( $prepareObject );
?>
Now all the above is fine and dandy to anyone familiar with using prepared statements, but if I want to use mysqli_fetch_fields or any other function that fetches meta information about a result set but does not work on prepared statements?
Enter the special function mysqli_stmt_result_metadata. It can be used as follows, assume the following code segment immediatley follows that of the above code segment.
<?php
$metaData = mysqli_stmt_result_metadata( $prepareObject );
// I Can Now Call mysqli_fetch_fields using the variable
// $metaData as an argument.
$fieldInfo = mysqli_fetch_fields( $metaData );
// Or Even This.
$fieldInfo = mysqli_num_fields( $metaData );
?>
Take a look at the Manual entry for mysqli_stmt_result_metatdata function for full details on how to expose it with prepared statements.
Good Luck,
20-Feb-2006 06:25
fetch_fields() does not seem to be compatible with prepared statements like those used here. Makes things difficult if you're using a wildcard. I guess that's better for security in some obscure way.
-Alex Boese
