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 — Stock un jeu de résultats depuis une requête préparée
Description
Style orienté objet (méthode) :
Style procédural :
Vous devez appeler mysqli_stmt_store_result() pour toutes les requêtes qui produisent un jeu de résultats (SELECT, SHOW, DESCRIBE, EXPLAIN), et uniquement si vous voulez stocker le jeu de résultats complet par le client, et donc, les séquences mysqli_stmt_fetch() pourront retourner ces données stockées.
Note: Il n'est pas nécessaire d'appeler mysqli_stmt_store_result() pour d'autres types de requête, mais si vous le faites, ce n'est pas grave et ne causera aucune perte notable de performance dans tous les cas. Vous pouvez détecter dans tous les cas si votre requête va produire un jeu de résultats en regardant si la fonction mysqli_stmt_result_metadata() retourne NULL.
Liste de paramètres
- stmt
-
Style procédural uniquement : Un identifiant de requête retourné par la fonction mysqli_stmt_init().
Valeurs de retour
Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec.
Exemples
Exemple #1 Style orienté objet
<?php
/* Ouvre la connexion */
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* Vérifie la connexion */
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = $mysqli->prepare($query)) {
/* Exécution de la requête */
$stmt->execute();
/* Stock le résultat */
$stmt->store_result();
printf("Nombre de lignes: %d.\n", $stmt->num_rows);
/* Libère le résultat */
$stmt->free_result();
/* Fermeture de la requête */
$stmt->close();
}
/* Fermeture de la connexion */
$mysqli->close();
?>
Exemple #2 Style procédural
<?php
/* Ouvre la connexion */
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* Vérifie la connexion */
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = mysqli_prepare($link, $query)) {
/* Exécution de la requête */
mysqli_stmt_execute($stmt);
/* Stock le résultat */
mysqli_stmt_store_result($stmt);
printf("Nombre de lignes : %d.\n", mysqli_stmt_num_rows($stmt));
/* Libère le résultat */
mysqli_stmt_free_result($stmt);
/* Fermeture de la requête */
mysqli_stmt_close($stmt);
}
/* Fermeture de la connexion */
mysqli_close($link);
?>
L'exemple ci-dessus va afficher :
Nombre de lignes : 20.
Voir aussi
- mysqli_prepare() - Prépare une requête SQL pour l'exécution
- mysqli_stmt_result_metadata() - Retourne les métadonnées de préparation de requête MySQL
- mysqli_stmt_fetch() - Lit des résultats depuis une requête MySQL préparée dans des variables liées
mysqli_stmt::store_result
29-Oct-2008 03:14
27-Dec-2006 11: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 08: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
