当结果集有不同的列时,是否有办法处理来自单个准备好的查询的多个结果集?
我有这样的程序:
CREATE PROCEDURE usp_CountAndList (in_SomeValue int)
BEGIN
SELECT COUNT(*) AS ListCount FROM Table WHERE SomeValue = in_SomeValue;
SELECT
Name, Cost, Text
FROM Table WHERE SomeValue = in_SomeValue
ORDER BY
Name
LIMIT 25;
END
我的 PHP 代码如下所示:
$some_value = $_POST["SomeValue"];
if($some_value != null) {
$dbh = mysqli_connect(...connection stuff...) or die ('I cannot connect to the database.');
$query = $dbh->prepare("CALL usp_CountAndList( ? );");
$query->bind_param("i", $some_value);
if($query->execute() == true) {
$meta = $query->result_metadata();
$fields = $meta->fetch_fields();
var_dump($fields);
$query->store_result();
$query->bind_result($list_count);
while($query->fetch()) {
print_r("<TR>");
print_r("<TD>" . $list_count ."</TD>");
print_r("</TR>\n");
}
$query->free_result();
$dbh->next_result();
$meta = $query->result_metadata();
$fields = $meta->fetch_fields();
var_dump($fields);
$query->store_result();
$query->bind_result($name, $cost, $text);
while($query->fetch()) {
print_r("<TR>");
print_r("<TD>" . $name . "</TD>");
print_r("</TR>\n");
}
$query->free_result();
$dbh->next_result();
}
else {
print_r("Query failed: " . $query->error . "<BR>\n");
exit(0);
}
$query->close();
$dbh->close();
}
我遇到的问题是,看起来我正在为第二个结果集获取相同的元数据,即使它返回一组完全不同的列,这意味着我的第二个 bind_result 调用结果在以下错误中:
PHP Warning: mysqli_stmt::bind_result() [<a href='mysqli-stmt.bind-result'>mysqli-stmt.bind-result</a>]: Number of bind variables doesn't match number of fields in prepared statement
我已经为此苦苦思索了一段时间,只是不清楚我做错了什么……这几乎像是一个 mysqli 错误。有没有人有一些示例代码来展示如何做我正在尝试的事情?
最佳答案
基本上有一些要求才能正常工作...
- > MYSQL 5.5.3 or higher
- > PHP 5.3 for mysqlnd support
- 这是一个编译时设置,所以如果您使用共享主机,您可能无法更改它。
基本上在您的示例中,使用 $query->next_result() 而不是 $dbh->next_result()。
mysqli_stmt::下一个结果
我只找到一个 SO example其他人试图这样做。
关于php - mysqli 处理具有不同列的多个结果集的准备过程调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29350782/