Jump to content

Recommended Posts

Fatal error: Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now in C:\xampp\htdocs\container\donation-clash\donation-clash.php:93 Stack trace: #0 C:\xampp\htdocs\container\donation-clash\donation-clash.php(93): mysqli->prepare('UPDATE accounts...') #1 {main} thrown in C:\xampp\htdocs\container\donation-clash\donation-clash.php on line 93

 

here is the part of the code. 

$stmt = $con->prepare("UPDATE accounts SET userbalance = ?, totaldonationdc = ?, userlevel = ?, userexperience = ?, totaleventjoined = ? WHERE id = ?");

//bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
$stmt->bind_param('iiidii', $userbalance, $totaldonationdc, $userlevel, $userexperience, $totaleventjoined, $_SESSION['id']);
 $stmt->execute();

i would like to know if there is a syntax error in this code or something. or if i can simply do this , anyways any help will be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/315295-commands-out-of-sync/
Share on other sites

PDO is more streamlined

  • mysqli has result objects and statement objects depending on whether you used query() or prepare(). The methods for handling the results of these two objects are completely different. This mean two confusing sets of methods to learn.
  • On the other hand, PDO produces objects with exactly the same methods to process the results of query() or prepare(), and usage of prepare() is itself much simpler.
  • Some mysqli methods are only available depending on your implementation of MySql.

PDO works with multiple varieties of DBMS (mysql, postgres etc). The SQL dialects may be different but your php code  remains the same if you change.

Having used the old mysql_* for years I originally changed to mysqli (how different could it be?) After a long while I decided I had to learn PDO in order to support others who were using it. I really wished I'd switched to it originally instead of mysqli.

  • Great Answer 1

i understand i will dive into pdo then. but for this command out of sync i am clueless since i didnt use mysqli_use_result or store_result maybe i should have in between my queries but now i will change to pdo . thank you for your answers.

41 minutes ago, alexandre said:

command out of sync

the most common cause is executing a query that returns a result set but not fetching all the data from that result set. this ties up the connection so you cannot run another query. therefore, if a query returns a result set, simply fetch all the data from the query in to an appropriately named  php variable, freeing up the connection for use by other queries, then test/use that variable throughout the rest of the code.

  • Like 1

in fact i am pretty sure that i am fetching everything at least everything before where the script breaks. if i used a store_result() before but removed it do the data could still be stored in memory or it goes when you remove the store_result()?

or you could just convert to the much simpler, better designed, and more modern pdo extension. 

in your previous thread, i posted an example showing what your SELECT query would look like using the pdo extension. here's what the UPDATE query in this thread would look like -

 

$stmt = $pdo->prepare("UPDATE accounts SET userbalance = ?, totaldonationdc = ?, userlevel = ?, userexperience = ?, totaleventjoined = ? WHERE id = ?");
$stmt->execute([ $userbalance, $totaldonationdc, $userlevel, $userexperience, $totaleventjoined, $_SESSION['id'] ]);

after you have made a connection using the pdo extension, converting existing msyqli based code to pdo mainly involves removing statements, copy/pasting the list of variables being bound into an array in the ->execute([...]) call, changing fetch statements (see pdo's fetch(), fetchAll() and sometimes fetchColumn()), and using differently spelled methods for things like last insert id and affected rows.

  • Like 1

amazing thank you very much for the example of update there nothing better than to see to learn for me so i appreciate that.

i will now convert to pdo. 

i would have a last question . with pdo will i still be able to make a ranking like that with a query or the way of  doing so is changing a bit? 

 $stmt = 'SELECT DISTINCT participationid, usernames, donationamount, totaldonated, sum(donationamount) as totaldonated  FROM donationclashdetails GROUP BY participationid ORDER BY totaldonated DESC';
     $result = mysqli_query($con, $stmt);
     if ((mysqli_num_rows($result) > 0) && mysqli_num_rows($result) < 1000) {
         while ($ROW = mysqli_fetch_assoc($result)) {
             echo $ROW['participationid']. $ROW['usernames']. $ROW['donationamount']. $ROW['totaldonated'] . ' - ' . '<br>';
					 }}

 

Edited by alexandre
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.