2013-03-12 46 views
7

这个简单的代码调用两个MySQL过程,但在第一个返回值之后,它会在第二个查询中返回一个错误。PHP命令不同步错误

注意:运行第一个或第二个将自己将正确返回每个。所以查询工作,只是不在一起。

完整的错误是: Invalid query: Commands out of sync; you can't run this command now

任何想法,请。

<?php 

require_once ('connection.php'); 
//First Query and Output 

$result = mysql_query("CALL C01_Client_Summary_ByAccount(1, '2012-02-27', '2013-03-29');"); 
if (!$result) { 
die('Invalid query: ' . mysql_error()); 
} 
while($row=mysql_fetch_array($result)) 
{ 
echo $row['CommisionPercentage']; 
} 

mysql_free_result($result); 
//END First Query and Output 

//Second Query and Output 
$new2 = mysql_query("CALL C01_Client_Summary_ByBetType(1, '2012-02-27', '2013-03-29');"); 
if (!$new2) { 
die('Invalid query: ' . mysql_error()); 
} 
while($row=mysql_fetch_array($new2)) 
{ 
echo $row['Turnover']; 
} 
//END Second Query and Output 

?> 
+1

请问您第一个'CALL()'原因2分的结果集? – Wrikken 2013-03-12 22:45:36

+0

我找到了这个解决方案 我需要将连接更改为 include('connection.php'); 然后在第一个查询关闭连接后 mysql_close($ con); 然后在第二个查询之前重新打开连接 include('connection.php'); – user2162372 2013-03-12 23:17:21

+0

请参阅http://stackoverflow.com/q/614671/632951 – Pacerier 2015-04-20 11:23:46

回答

5

PHP的旧MySQL扩展与存储过程无法正常工作。不幸的是,接缝是无法执行多个存储过程的。问题是第一个过程留下了一些导致第二个失败的缓冲结果集。但是,您可以使用mysqli扩展。这里是如何做到这一点一个很好的例子:

http://www.daniweb.com/web-development/php/threads/234868/error-commands-out-of-sync-you-cant-run-this-command-now

+0

谢谢 - 我发现也 - 我的解决方案可能是作弊 – user2162372 2013-03-12 23:21:53

+1

解决方案是切换到'mysqli'或'PDO'扩展名。请记住,旧的'mysql_xxX()'函数已被弃用且不受支持;有一天你需要升级你的PHP版本,如果你还没有切换,你会发现你的代码根本不起作用;当发生这种情况时,修复它将是一个真正的痛苦。现在切换,而它很容易做到。 – Spudley 2013-03-12 23:37:13