[英]Display the result table for 2 sql queries in php
我正在写一个php文件,我想通过执行2个独立的查询来显示两个表,并将它们存储在$ result和$ result_bike中。 但是,当我尝试打开此操作表单的html页面时,它仅显示第一个查询的表,并在第二个位置显示错误“命令不同步;您现在不能运行此命令”表。
另外,我不想合并这两个表,因为它们显示了完全不同的信息,并且我想插入一些解释每个表的文本。
我认为可能与无法为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文档的用户评论。 希望这可以帮助
调用多个存储过程时,可能会遇到以下错误:“命令不同步;您现在不能运行此命令”。 即使在两次调用之间对结果对象使用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();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.