18

複数の結果セットを持つストアドプロシージャがあります。これらの結果を取得するには、mysqliの2番目の結果セットに進むにはどうすればよいですか?

次のようなストアドプロシージャだとしましょう。

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で機能するようになりましたが、何らかの理由ですべてのintが空白の文字列として返されます。

他の誰かがこれに出くわし、解決策を持っていますか?

4

3 に答える 3

17

ここで何かが足りないと思います。これは、mysqliプリペアドステートメントを使用してストアドプロシージャから複数の結果を取得する方法です。

$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_stmt_get_result($stmt);
// 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.

//move to next result set
mysqli_stmt_next_result($stmt);
$result2 = mysqli_stmt_get_result($stmt);
// 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.

// 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']);
}

ちなみに、手続き型は意図的に使っていますか?でオブジェクト指向スタイルを使用mysqliすると、コードがもう少し魅力的に見えるようになります(私の個人的な意見)。

于 2009-11-06T07:45:11.403 に答える
1

オブジェクトはオブジェクトとは異なる動作をするためmysqli_multi_query()、MySQLi は を介し​​てのみ複数の結果セットをサポートしているようです。MySQLi_STMTMySQLi_Result

PDO は、通常のクエリ ( ) と準備されたステートメント ( )の両方で複数の結果セットを処理できるPDOStatementオブジェクトにより、多少抽象化されているようです。PDO::queryPDO:prepare

于 2009-11-05T22:04:57.673 に答える