gpt4 book ai didi

php - 在php中显示2条sql查询的结果表

转载 作者:行者123 更新时间:2023-11-29 09:55:51 25 4
gpt4 key购买 nike

我正在编写一个 php 文件,我想通过执行 2 个单独的查询来显示两个表,并将它们存储在 $result 和 $result_bike 中。但是,当我尝试打开此操作表单的 html 页面时,它仅显示第一个查询的表,并在第二个查询的位置给出错误“命令不同步;您现在无法运行此命令” table 。

此外,我不想合并这两个表,因为它们显示完全不同的信息,我想插入一些文本来解释每个表。

我认为可能与无法为 php 打印两个表有关(我对此表示怀疑)?我应该做出什么改变?

提前感谢您的帮助!

        $result = mysql_query("CALL CrashTypeRate_Ped('$city')", $conn);
if (!$result){
echo "Fail to retrieve result for pedestrian crashes!\n";
print mysql_error();
} else {
echo "<table border=1>\n";
echo "<tr><td>CrashType</td><td>Count</td><td>TotalCount</td></tr>\n";
while ($myrow = mysql_fetch_array($result)) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow["crash_type"], $myrow["type_count"], $myrow["total_count"]);
}
echo "</table>\n";
}


$result_bike = mysql_query("CALL CrashTypeRate_Bike('$city')", $conn);
if (!$result_bike) {
echo "Fail to retrieve result for bike crashes!\n";
print mysql_error();
} else {
echo "got here!!!!!!";
echo "<table border=1>\n";
echo "<tr><td>CrashType</td><td>Count</td><td>TotalCount</td></tr>\n";
while ($myrow = mysql_fetch_array($result_bike)) {
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n", $myrow["crash_type"], $myrow["type_count"], $myrow["total_count"]);
}
echo "</table>\n";
}

最佳答案

这里来自 PHP 文档用户评论。希望这有帮助

Link to PHP Documentation

调用多个存储过程时,您可能会遇到以下错误:“命令不同步;您现在无法运行此命令”。即使在调用之间对结果对象使用 close() 函数时,也可能会发生这种情况。要解决此问题,请记住在每次存储过程调用后调用 mysqli 对象上的 next_result() 函数。请参阅下面的示例:

<?php
// New Connection
$db = new mysqli('localhost','user','pass','database');

// Check for errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}

// 1st Query
$result = $db->query("call getUsers()");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$user_arr[] = $row;
}
// Free result set
$result->close();
$db->next_result();
}

// 2nd Query
$result = $db->query("call getGroups()");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$group_arr[] = $row;
}
// Free result set
$result->close();
$db->next_result();
}
else echo($db->error);

// Close connection
$db->close();
?>

关于php - 在php中显示2条sql查询的结果表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53877599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com