调用多个存储过程时出现PHP MYSQL错误
当我在一个页面中多次调用过程时,调用和显示内容会有困难。我试图显示MYSQL的两个不同SP调用中的两个独立记录集。我可以显示第一个呼叫,但第二个呼叫失败。我不确定我做错了什么,但也许有人能帮忙 当我调用第二个过程时,我不断得到错误:调用多个存储过程时出现PHP MYSQL错误,php,stored-procedures,mysqli,Php,Stored Procedures,Mysqli,当我在一个页面中多次调用过程时,调用和显示内容会有困难。我试图显示MYSQL的两个不同SP调用中的两个独立记录集。我可以显示第一个呼叫,但第二个呼叫失败。我不确定我做错了什么,但也许有人能帮忙 当我调用第二个过程时,我不断得到错误: 调用不同步的SPC命令时出错;现在无法运行此命令 我在windows上运行 下面的代码。。。PHP // First call to SP $page = 2; $section = 1; include("DatabaseConnection.php"); /
调用不同步的SPC命令时出错;现在无法运行此命令
我在windows上运行
下面的代码。。。PHP
// First call to SP
$page = 2;
$section = 1;
include("DatabaseConnection.php"); //general connection - works fine
$sql = 'CALL GetPageContent("'.$page.'", "'.$section.'")';
$result = mysqli_query($conn, $sql) or die('Error calling SP' .mysqli_error($conn));
while($row=mysqli_fetch_assoc($result))
{
// DO STUFF< REMOVED TO MAKE READING CLEARER
}
mysqli_free_result($result);
//SECOND CALL BELOW
$section = 2; // change parameter for different results
$sql = 'CALL GetPageContent("'.$page.'", "'.$section.'")';
$result = mysqli_query($conn, $sql) or die('Error calling SP' .mysqli_error($conn));
while($row=mysql_fetch_assoc($result))
{
// DO STUFF< REMOVED TO MAKE READING CLEARER
}
//对SP的第一次调用
$page=2;
$section=1;
包括(“DatabaseConnection.php”)//一般连接-工作正常
$sql='调用GetPageContent(“.$page.”、“.$section.”);
$result=mysqli_query($conn,$sql)或die('Error calling SP'.mysqli_Error($conn));
while($row=mysqli\u fetch\u assoc($result))
{
//做些事情让阅读更清晰
}
mysqli_免费_结果($result);
//下面的第二个电话
$section=2;//更改参数以获得不同的结果
$sql='调用GetPageContent(“.$page.”、“.$section.”);
$result=mysqli_query($conn,$sql)或die('Error calling SP'.mysqli_Error($conn));
while($row=mysql\u fetch\u assoc($result))
{
//做些事情让阅读更清晰
}
要解决此问题,请记住在每次存储过程调用后调用mysqli对象上的下一个_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();
?>
如果要在单个脚本页面中调用多个过程,则应在调用另一个过程之前调用mysqli\u next\u result($connection\u link)。 考虑下面的示例代码块:
第二次抓取不应该是
mysqli\u fetch\u assoc
?是的,但无论哪种方式,我仍然会得到相同的错误?这不管用吗?这很好,我不再得到任何错误,但我如何在while循环中显示以下内容。。。非常感谢你的下一步。。。我以前在while循环中调用了以下内容,但现在我得到一个错误:致命错误:无法将stdClass类型的对象用作echo“