PHP 8.3.4 Released!

mysqli::store_result

mysqli_store_result

(PHP 5, PHP 7, PHP 8)

mysqli::store_result -- mysqli_store_resultÜberträgt die Ergebnismenge der letzten Abfrage

Beschreibung

Objektorientierter Stil

public mysqli::store_result(int $mode = 0): mysqli_result|false

Prozeduraler Stil

mysqli_store_result(mysqli $mysql, int $mode = 0): mysqli_result|false

Überträgt die Ergebnismenge der letzten Abfrage über die durch mysql angegebene Datenbankverbindung, um sie mit der Funktion mysqli_data_seek() zu verwenden.

Parameter-Liste

mysql

Nur bei prozeduralem Aufruf: ein von mysqli_connect() oder mysqli_init() zurückgegebenes mysqli-Objekt.

mode

Die Option, die gesetzt werden soll. Ab PHP 8.1 hat dieser Parameter keine Auswirkung mehr. Es kann einer der folgenden Werte sein:

Gültige Optionen
Name Beschreibung
MYSQLI_STORE_RESULT_COPY_DATA Kopiert die Ergebnisse aus dem internen mysqlnd-Puffer in die abgerufenen PHP-Variablen. Um zu vermeiden, dass im Speicher gehaltene Ergebnisse kopiert und dupliziert werden, verwendet mysqlnd standardmäßig Referenzen. In bestimmten Fällen, z. B. bei Ergebnismengen mit vielen kleinen Zeilen, kann das Kopieren den Gesamtspeicherverbrauch verringern, weil die PHP-Variablen, die Ergebnisse enthalten, früher freigegeben werden können (nur mit mysqlnd verfügbar).

Rückgabewerte

Gibt ein gepuffertes Ergebnisobjekt zurück oder false, wenn ein Fehler aufgetreten ist.

Hinweis:

Wenn die Abfrage keine Ergebnismenge zurückgegeben hat (z. B. wenn die Abfrage eine INSERT-Anweisung war), gibt die Funktion false zurück. Diese Funktion gibt auch false zurück, wenn die Ergebnismenge nicht gelesen werden konnte. Ob ein Fehler aufgetreten ist, kann überprüft werden, indem kontrolliert wird, ob mysqli_error() eine nicht-leere Zeichenkette zurückgibt, ob mysqli_errno() einen Wert ungleich Null zurückgibt oder ob mysqli_field_count() einen Wert ungleich Null zurückgibt. Ein weiterer möglicher Grund dafür, dass diese Funktion nach einem erfolgreichen Aufruf von mysqli_query() false zurückgibt, kann eine zu große Ergebnismenge sein (der dafür benötigte Speicher kann nicht zugewiesen werden). Wenn mysqli_field_count() einen Wert ungleich Null zurückgibt, sollte die Anweisung eine nicht-leere Ergebnismenge erzeugt haben.

Fehler/Exceptions

If mysqli error reporting is enabled (MYSQLI_REPORT_ERROR) and the requested operation fails, a warning is generated. If, in addition, the mode is set to MYSQLI_REPORT_STRICT, a mysqli_sql_exception is thrown instead.

Beispiele

Siehe mysqli_multi_query().

Anmerkungen

Hinweis:

Obwohl es immer sinnvoll ist, den vom Ergebnis einer Abfrage belegten Speicher mit der Funktion mysqli_free_result() freizugeben, ist dies besonders wichtig, wenn große Ergebnismengen mit der Funktion mysqli_store_result() übertragen werden.

Siehe auch

add a note

User Contributed Notes 5 notes

up
10
mitchind
14 years ago
After reading through original notes and example above as well as wading through the documentation, I finally got a loop to work with two stored procedures.

Using the results of the first one as a parameter for the second one. Easier to do this way than a huge modified sequence of Inner Join queries.

Hope this helps others...

<?php
// Connect to server and database
$mysqli = new mysqli("$dbServer", "$dbUser", "$dbPass", "$dbName");

// Open First Stored Procedure using MYSQLI_STORE_RESULT to retain for looping
$resultPicks = $mysqli->query("CALL $proc ($searchDate, $maxRSI, $incRSI, $minMACD, $minVol, $minTrades, $minClose, $maxClose)", MYSQLI_STORE_RESULT);

// process one row at a time from first SP
while($picksRow = $resultPicks->fetch_array(MYSQLI_ASSOC)) {
// Get Parameter for next SP
$symbol = $picksRow['Symbol'];

// Free stored results
clearStoredResults($mysqli);

// Execute second SP using value from first as a parameter (MYSQLI_USE_RESULT and free result right away)
$resultData = $mysqli->query("CALL prcGetLastMACDDatesBelowZero('$symbol', $searchDate)", MYSQLI_USE_RESULT);
$dataRow = $resultData->fetch_array(MYSQLI_ASSOC);

// Dump result from both related queries
echo "<p>$symbol ... Num Dates: " . $dataRow['NumDates'];

// Free results from second SP
$resultData->free();

}

// Free results from first SP
$resultPicks->free();

// close connections
$mysqli->close();

#------------------------------------------
function clearStoredResults($mysqli_link){
#------------------------------------------
while($mysqli_link->next_result()){
if(
$l_result = $mysqli_link->store_result()){
$l_result->free();
}
}
}
?>
up
2
filippo at ecoms dot it
6 years ago
Code to handling errors:

if ($mysqli->multi_query($query)) {
$result = $mysqli->store_result();
if ($mysqli->errno == 0) {

/* First result set or FALSE (if the query didn't return a result set) is stored in $result */

while ($mysqli->more_results()) {
if ($mysqli->next_result()) {
$result = $mysqli->store_result();
if ($mysqli->errno == 0) {
/* The result set or FALSE (see above) is stored in $result */
}
else {
/* Result set read error */
break;
}
}
else {
/* Error in the query */
}
}
}
else {
/* First result set read error */
}
}
else {
/* Error in the first query */
}
up
-3
Warner
14 years ago
It also seems, that executing a SET statement in multi_query() returns an extra recordset too, which one would not expect.
up
-6
Tex Morgan
10 years ago
There's a simpler way to clear out database stored procedure problems:

class MySQLiQuery {
protected $_resultSet;
protected $databaseConnection;
....

protected function free(){
$this->_resultSet->free();
$this->_resultSet=null;
}

protected function checkMoreResults(){
if($this->databaseConnection->more_results()){
return true;
} else {
return false;
}
}

protected function clearResults(){
if($this->checkMoreResults()){
if($this->databaseConnection->next_result()){
if($this->_resultSet=$this->databaseConnection->store_result()){
$this->free();
}
$this->clearResults(); // <----------- recursive call is your friend
}
}
}
.......
}
up
-5
lau at goldenweb dot com dot au
16 years ago
Beware when using stored procedures:
If you connect to the database and then call dbproc A followed by a call to db proc B and then close the connection to the db, the second procedure call will not work.

It looks like there is a bug in MYSQL or mysqli that returns an extra recordset than you would expect. It then doesn't let you call another stored procedure until you finish processing all the recordsets from the first stored procedure call.

The solution is to simply loop through the additional recordsets between calls to db procs. Here is a function that I call between db proc calls:

<?php
#--------------------------------
function ClearRecordsets($p_Result){
#--------------------------------
$p_Result->free();
while(
$this->Mysqli->next_result()){
if(
$l_result = $this->Mysqli->store_result()){
$l_result->free();
}
}
}

?>
To Top