944,180 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 6028
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2009
0

Error: Commands out of sync; you can't run this command now

Expand Post »
i m getting this bug.. and i still cant understand why this happening..??

here is my php code

php Syntax (Toggle Plain Text)
  1.  
  2. $query1 = "call new_user('$cardDigits','$cardNo','$amount','$traiff','','','','','$creator',@_lastname,'$customer','$firstName','$email','0','0')";
  3. $result1 = mysql_query($query1) or die('query_error'.''.mysql_error());
  4. $lastname = mysql_fetch_row($result1);
  5.  
  6. // Generate New User
  7. $query2 = "genrate_user('$lastname[0] ','$creator')";
  8. echo $query2;
  9. $result2 = mysql_query($query2) or die('query_error1'.''.mysql_error());

Procedures are working fine..!!!
1st Procedure generate the $lastname
which is the input parameter of second Procedure..!!!!!

when i print or echo the 2nd procedure.. its run fine at mysql.. but through php its throughing error Commands out of sync; you can't run this command now

Help me guys..!!!
Last edited by sam023; Oct 31st, 2009 at 7:24 am.
Reputation Points: 11
Solved Threads: 6
Junior Poster
sam023 is offline Offline
164 posts
since Jun 2009
Oct 31st, 2009
0
Re: Error: Commands out of sync; you can't run this command now
strange no replies uptil now..!!

is this error occur rarely.?? or no one knows about it.??
Reputation Points: 11
Solved Threads: 6
Junior Poster
sam023 is offline Offline
164 posts
since Jun 2009
Oct 31st, 2009
0
Re: Error: Commands out of sync; you can't run this command now
Hey.

The old MySQL extension was never built to run procedures, even tho it *can* be used to do so. You should be using the Improved MySQL extension if you are planing to use features like Stored Procedures.

The problem you are facing, "Commands out of sync", is caused by unused result sets left over by your procedure. When you call your first procedure, the result sets are buffered until you use them. However, you only use one set, and you didn't even free it before moving on to the second query. You need to free the buffered result sets before moving on:

It is best to create a function, or a method, to do this. No need to repeat the code over and over.
For example:
php Syntax (Toggle Plain Text)
  1. <?php
  2. /**
  3.  * Calls a Stored Procedure and returns the results as an array of rows.
  4.  * @param mysqli $dbLink An open mysqli object.
  5.  * @param string $procName The name of the procedure to call.
  6.  * @param string $params The parameter string to be used
  7.  * @return array An array of rows returned by the call.
  8.  */
  9. function c_mysqli_call(mysqli $dbLink, $procName, $params="")
  10. {
  11. if(!$dbLink) {
  12. throw new Exception("The MySQLi connection is invalid.");
  13. }
  14. else
  15. {
  16. // Execute the SQL command.
  17. // The multy_query method is used here to get the buffered results,
  18. // so they can be freeded later to avoid the out of sync error.
  19. $sql = "CALL {$procName}({$params});";
  20. $sqlSuccess = $dbLink->multi_query($sql);
  21.  
  22. if($sqlSuccess)
  23. {
  24. if($dbLink->more_results())
  25. {
  26. // Get the first buffered result set, the one with our data.
  27. $result = $dbLink->use_result();
  28. $output = array();
  29.  
  30. // Put the rows into the outpu array
  31. while($row = $result->fetch_assoc())
  32. {
  33. $output[] = $row;
  34. }
  35.  
  36. // Free the first result set.
  37. // If you forget this one, you will get the "out of sync" error.
  38. $result->free();
  39.  
  40. // Go through each remaining buffered result and free them as well.
  41. // This removes all extra result sets returned, clearing the way
  42. // for the next SQL command.
  43. while($dbLink->more_results() && $dbLink->next_result())
  44. {
  45. $extraResult = $dbLink->use_result();
  46. if($extraResult instanceof mysqli_result){
  47. $extraResult->free();
  48. }
  49. }
  50.  
  51. return $output;
  52. }
  53. else
  54. {
  55. return false;
  56. }
  57. }
  58. else
  59. {
  60. throw new Exception("The call failed: " . $dbLink->error);
  61. }
  62. }
  63. }
  64. ?>

Which you could use like:
php Syntax (Toggle Plain Text)
  1. <?php
  2. header('content-type: text/plain');
  3.  
  4. $dbLink = new mysqli('localhost', 'usr', 'pwd', 'dbname');
  5.  
  6. // Execute the first call
  7. echo "\n--- FIRST CALL ---\n";
  8. $result = c_mysqli_call($dbLink, 'TestProc', "2, 'second param'");
  9. if($result) {
  10. echo "Output: \n";
  11. foreach($result as $_row) {
  12. echo " " . $_row['something'] . "\n";
  13. }
  14. }
  15.  
  16. // Execute the second call
  17. echo "\n--- SECOND CALL ---\n";
  18. $result = c_mysqli_call($dbLink, 'TestProc', "3, 'second param'");
  19. if($result) {
  20. echo "Output: \n";
  21. foreach($result as $_row) {
  22. echo " " . $_row['something'] . "\n";
  23. }
  24. }
  25. ?>
Hope that helps.
Last edited by Atli; Oct 31st, 2009 at 3:03 pm. Reason: Spell-checker phail.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 2nd, 2009
0
Re: Error: Commands out of sync; you can't run this command now
If it only matters to free the buffered result set..!!!!
Can i use mysql_free_result() or is there any other way to do this thing without using mysqli..???
Last edited by sam023; Nov 2nd, 2009 at 12:46 am.
Reputation Points: 11
Solved Threads: 6
Junior Poster
sam023 is offline Offline
164 posts
since Jun 2009
Nov 2nd, 2009
1
Re: Error: Commands out of sync; you can't run this command now
As far as I know, there is no way for the old MySQL extension to execute multiple procedures. Not without using multiple connections, which is a horrific idea.
You would have to be able to fetch multiple result sets, and I don't see a way to do that with the mysql_ functions. It would need to implement the mysql_next_result() function of the MySQL API.

Like I say, the mysql extension was not built to be used with procedures. It was built for MySQL 3, but stored procedures weren't introduced until MySQL 5. The Improved MySQL extension was created to address these sort of incompatibilities.

Any specific reason you do not want to switch over?
Last edited by Atli; Nov 2nd, 2009 at 1:03 am. Reason: Added a bit more detail.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 2nd, 2009
-1
Re: Error: Commands out of sync; you can't run this command now
i dont want to switch to mysqli because it is most creepiest thing in php according to me..!!! i wasted hours to install it.. but was unable to that..!!!

i uncommented the extension=php_mysqli.dll in php.ini

also check mysqli dll files in ext folder..

but still i m unable to install mysqli...

every time i got this error Class 'mysqli' not found

i m using XAMPP 1.7.2 for windows..!!!
Last edited by sam023; Nov 2nd, 2009 at 1:43 am.
Reputation Points: 11
Solved Threads: 6
Junior Poster
sam023 is offline Offline
164 posts
since Jun 2009
Nov 2nd, 2009
0
Re: Error: Commands out of sync; you can't run this command now
Well, I don't know about XAMPP as such, but the process of installing PHP extensions is fairly simple:
  1. Put the DLL into the ext/ directory.
  2. Add/Uncomment the "extension=xxx.dll" line in the config.
  3. Restart Apache.
  4. ... and your done.

If that fails, the most common causes are:
  • You are editing the incorrect php.ini file.
  • Windows can not find the ext/ directory. Or, more accurately, it doesn't know it is supposed to look in the PHP directory.
  • Prerequisites for the extension are missing. (Shouldn't be the case for you, seeing as the other MySQL extension is working.)
  • You are using Windows! (Joking... kind of ;-)
For the first two, the solution is usually to put the PHP directory into the PATH variable.
See this page for details on how to do that.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 2nd, 2009
0
Re: Error: Commands out of sync; you can't run this command now
That's odd... I just installed XAMPP 1.7.2 on my Windows test box and the mysqli extension was enabled by default.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 2nd, 2009
0
Re: Error: Commands out of sync; you can't run this command now
Yeah it is default uncommented
stupid of me..!! actually i m using Nusphere at my php IDE..!!!
i dont know tht Nushpere also have php.ini.. in it is commented mysqli.dll

i un comment that and now its working..!!

i have a question regarding mysqli...!!!
if i made my config file in mysql...!! than can i use mysqli functions..!!!
i mean is it necessary to make connection with mysqli extension to use mysqli function..!!!
Last edited by sam023; Nov 2nd, 2009 at 3:08 am.
Reputation Points: 11
Solved Threads: 6
Junior Poster
sam023 is offline Offline
164 posts
since Jun 2009
Nov 2nd, 2009
0
Re: Error: Commands out of sync; you can't run this command now
Yuppy..!!! i got the output finally..!!!

thanks to u bro..!!!

but in coding i made two config file.. one with mysql other with msqli..!!!

php Syntax (Toggle Plain Text)
  1. $query1 = "call new_user('$cardDigits','$cardNo','$amount','$traiff','','','','','$creator',@_lastname,'$customer','$firstName','$email','0','0')";
  2. $result1 = mysqli_query(,$mysqli,$query1) // here i made change
  3. $lastname = mysqli_fetch_row($result1); // here i made change
  4.  
  5. // Generate New User
  6. $query2 = "genrate_user('$lastname[0] ','$creator')";
  7. echo $query2;
  8. $result2 = mysql_query($query2) or die('query_error1'.''.mysql_error());

thats it..!!
i know its a bad pratice..!! but i will improve it..!!
can u give link to basic tutorial of mysqli..?

i will appreciate that.!!
Last edited by sam023; Nov 2nd, 2009 at 3:48 am.
Reputation Points: 11
Solved Threads: 6
Junior Poster
sam023 is offline Offline
164 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Writing form.database output to xml
Next Thread in PHP Forum Timeline: Blowfish Hashing?!?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC