I made a question about this before and added a bounty, but I put the wrong code I just realized.
$getgames = $db->prepare("SELECT `id`, `name`, `picture` FROM `games` WHERE `status` = '1' ORDER BY `id` ASC");
$getgames->execute();
$getgames->bind_result($gid, $name, $picture);
while($getgames->fetch()){
$getgames->free_result();
$gettokens = $db->prepare("SELECT SUM(`amount`) AS `tokenamount` FROM `tokens` WHERE `game` = ? AND `user` = ?");
$gettokens->bind_param('ii', $gid, $sesid);
$gettokens->execute();
$gettokens->bind_result($tokenamount);
$gettokens->fetch();
This code returns the first row correctly, but then for the second it doesn't show it and returns the following error.
Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Attempt to read a row while there is no result set associated with the statement'
The line that it refers to is the one with $getgames->execute();
.
Is it an error with the free_result? I'm still learning MySQLi so I have lots of problems with this.
UPDATE:
When I remove $getgames->free_result();
then I get a new error of Commands out of sync; you can't run this command now
. On the line $getgames->bind_result($gid, $name, $picture);