r/PHPhelp • u/gr00316 • Oct 27 '25
Command out of Sync when moving from 7 to 8
I have code that I'll post below. Tried to switch servers and getting a command out of sync fatal error. I've done research and it seems people had this issue even before 8 came out and it seems like what everything is saying to do is actually what I'm doing.
$query= "SELECT NoticeMessage, DisplayUntil FROM HomepageNotice WHERE RecordID=1";
$stmt = $db->prepare($query);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($NoticeMessage, $DisplayUntil);
$query1= "SELECT ImportantTopic1, ImportantTopic2, ImportantTopic3, ImportantTopic4, ImportantTopic5, ImportantTopic6 FROM ImportantTopics WHERE RecordID=1";
$stmt1 = $db->prepare($query1);
$stmt1->execute();
$stmt1->store_result();
$stmt1->bind_result($ImportantTopic1, $ImportantTopic2, $ImportantTopic3, $ImportantTopic4, $ImportantTopic5, $ImportantTopic6);
$query2= "SELECT RecordID, NewsTitle, Brief, PhotoURLThumb FROM NewsStories WHERE DIST='Y' ORDER BY Date Desc LIMIT 18";
$stmt2 = $db->prepare($query2);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($NewsStoryID, $NewsTitle, $Brief, $PhotoURLThumb);
$query3= "SELECT SliderTitle, SliderBrief, SliderURL, SliderShort, SliderPhoto, SliderAlt FROM Slider";
$stmt3 = $db->prepare($query3);
$stmt3->execute();
$stmt3->store_result();
$stmt3->bind_result($SliderTitle, $SliderBrief, $SliderURL, $SliderShort, $SliderPhoto, $SliderAlt);
Then later in the code I call
while($stmt->fetch()) {
echo "I display the results here";
$stmt->free_result(); //ADDED THIS LINE TO EDIT THIS IS THE LINE CAUSING THE ISSUE I FOUND OUT
}
That while loop works fine on PHP 7.3 but not on 8.3. But I'm using store results so I'm wondering if it's a PHP setting issue?
EDIT: Found what was causing the issue. I had a stmt-> free results in the loop, once that line was removed or the moved outside the loop the code ran fine.
8
u/allen_jb Oct 27 '25
A significant change in PHP 8.1 for mysqli is that error reporting now defaults to exceptions (see https://www.php.net/manual/en/mysqli-driver.report-mode.php )
Most likely the code did not actually "work fine" before - you were just ignoring (not explicitly checking) for errors.
For more information on this error specifically, see "Command out of sync" in the MySQL manual.
Unfortunately I can't help much further as you're using a pattern I don't usually use (I would immediately fetch the results into PHP variables rather than keeping them stored in the statement objects - you're already telling PHP to store the result on the client-side, and from the code I can see here I'm guessing you're only dealing with tens of records at most here, so there's likely no to minimal memory usage impact).
I believe it's possible that you're doing something else between the code where you execute the statements and where you run the while loop that's causing the problem.
2
u/lovesrayray2018 Oct 27 '25
Its because mysql expects you to use the results of a query before you start executing a new one using the same database pointer, or specifically release the memory using free_result between queries if you do not want to use the previous result set.
If you want to have multiple queries executed in parallel using one database pointer, but create one single result set, you should be using multi_query instead. https://www.php.net/manual/en/mysqli.multi-query.php
1
u/supergnaw Oct 27 '25
I have never used store_result before but in the docs it says it's meant to be used with the data_seek method. Might be a good place to start searching for your issue.
1
u/obstreperous_troll Oct 27 '25
I highly suggest using PDO and using $stmt->fetchAll() which is pretty much what store_result does, except the latter has a strange API unique to mysqli.
1
u/MateusAzevedo Oct 27 '25
I've never seen this error before, but my guess is you need
->get_result()->fetch_all()before sending another query.