Получение наборов множественного результата с хранимой процедурой в php/mysqli
У меня есть хранимая процедура с несколькими наборами результатов. Как мне перейти на второй результат в mysqli, чтобы получить эти результаты?
Скажем, это хранимая процедура вроде:
create procedure multiples( param1 INT, param2 INT )
BEGIN
SELECT * FROM table1 WHERE id = param1;
SELECT * FROM table2 WHERE id = param2;
END $$
PHP выглядит примерно так:
$stmt = mysqli_prepare($db, 'CALL multiples(?, ?)');
mysqli_stmt_bind_param( $stmt, 'ii', $param1, $param2 );
mysqli_stmt_execute( $stmt );
mysqli_stmt_bind_result( $stmt, $id );
Тогда это та часть, с которой я не могу работать. Я попытался использовать mysqli_next_result для перехода к следующему набору результатов, но не могу заставить его работать. Мы получили его для работы с mysqli_store_result и mysqli_fetch_assoc/array/row, но по какой-то причине все ints возвращаются в виде пустых строк.
Кто-нибудь еще сталкивается с этим и имеет решение?
Ответы
Ответ 1
Я думаю, что вам что-то не хватает (следующее не было протестировано):
$stmt = mysqli_prepare($db, 'CALL multiples(?, ?)');
mysqli_stmt_bind_param($stmt, 'ii', $param1, $param2);
mysqli_stmt_execute($stmt);
// fetch the first result set
$result1 = mysqli_use_result($db);
// you have to read the result set here
while ($row = $result1->fetch_assoc()) {
printf("%d\n", $row['id']);
}
// now we're at the end of our first result set.
mysqli_free_result($result1);
//move to next result set
mysqli_next_result($db);
$result2 = mysqli_use_result($db);
// you have to read the result set here
while ($row = $result2->fetch_assoc()) {
printf("%d\n", $row['id']);
}
// now we're at the end of our second result set.
mysqli_free_result($result2);
// close statement
mysqli_stmt_close($stmt);
Используя PDO
, ваш код будет выглядеть так:
$stmt = $db->prepare('CALL multiples(:param1, :param2)');
$stmt->execute(array(':param1' => $param1, ':param2' => $param2));
// read first result set
while ($row = $stmt->fetch()) {
printf("%d\n", $row['id']);
}
$stmt->nextRowset();
// read second result set
while ($row = $stmt->fetch()) {
printf("%d\n", $row['id']);
}
Но я слышал, что PDOStatement::nextRowset()
не реализован с MySQL PDO, что делает невозможным получение нескольких наборов результатов:
Итак, в зависимости от вашей версии PHP вам придется придерживаться mysqli
-решения. Кстати: вы намеренно используете процедурный стиль? Использование объектно-ориентированного стиля с помощью mysqli
сделает ваш код более привлекательным (мое личное мнение).
Ответ 2
Похоже, MySQLi может поддерживать только несколько наборов результатов через mysqli_multi_query()
, поскольку объекты MySQLi_STMT
работают иначе, чем объекты MySQLi_Result
.
PDO представляется несколько более абстрактным, при этом объекты PDOStatement могут обрабатывать несколько наборов результатов для обоих регулярных запросов (PDO::query
) и подготовленные операторы (PDO:prepare
).
Ответ 3
Это сработало очень хорошо для меня, оно будет иметь дело с (как пример) так же, как и множество списков выбора, как в вашем SP. Обратите внимание, как вам нужно закрыть $call, прежде чем вы сможете перейти к параметрам OUT из вашего SP...
?><pre><?
$call = mysqli_prepare($db, 'CALL test_lists(?, ?, @result)');
if($call == false) {
echo "mysqli_prepare (\$db, 'CALL test_lists(?, ?, @result) FAILED!!!\n";
} else {
// A couple of example IN parameters for your SP...
$s_1 = 4;
$s_2 = "Hello world!";
// Here we go (safer way of avoiding SQL Injections)...
mysqli_stmt_bind_param($call, 'is', $s_1, $s_2);
// Make the call...
if(mysqli_stmt_execute($call) == false) {
echo "mysqli_stmt_execute(\$call) FAILED!!!\n";
} else {
//print_r($call);
// Loop until we run out of Recordsets...
$set = 0;
while ($recordset = mysqli_stmt_get_result($call)) {
++$set;
//print_r($recordset);
echo "\nRecordset #" . $set . "...\n";
if ($recordset->num_rows > 0) {
$ctr = 0;
while ($row = $recordset->fetch_assoc()) {
++$ctr;
//print_r($row);
echo "\t" . $ctr . ": ";
forEach($row as $key => $val) {
echo "[" . $key . "] " . $val . "\t";
}
echo "\n";
}
}
echo $recordset->num_rows . " record" . ($recordset->num_rows == 1 ? "" : "s") . ".\n";
// Clean up, ready for next iteration...
mysqli_free_result($recordset);
// See if we can get another Recordset...
mysqli_stmt_next_result($call);
}
// Then you have to close the $call...
mysqli_stmt_close($call);
// ...in order to get to the SP OUT parameters...
$select = mysqli_query($db, "SELECT @result");
$row = mysqli_fetch_row($select);
$result = $row[0];
echo "\nOUT @result = " . $result . "\n";
}
}
?></pre><?
И вот что получается из вышеприведенного кода, используя мой test_lists SP...
Recordset #1...
1: [s_1] 4 [user_name] Andrew Foster
2: [s_1] 4 [user_name] Cecil
3: [s_1] 4 [user_name] Sheff
3 records.
Recordset #2...
1: [s_2] Hello world! [section_description] The Law
2: [s_2] Hello world! [section_description] History
3: [s_2] Hello world! [section_description] Wisdom Literature
4: [s_2] Hello world! [section_description] The Prophets
5: [s_2] Hello world! [section_description] The Life of Jesus and the Early Church
6: [s_2] Hello world! [section_description] Letters from the Apostle Paul
7: [s_2] Hello world! [section_description] Other Letters from Apostles and Prophets
8: [s_2] Hello world! [section_description] Prophecy - warnings for the present and revelation of the future
8 records.
OUT @result = 16