currently I edit my code from mysqli style to mysqli_stmt in order to be able to have better protection against SQL injections. I rewrote my code but when I want to inspect how many rows a certain query has, I receive this error: Commands out of sync; you can't run this command now, so my table will not show any data, but in reality it has.
I have read about this error for hours, but I do not know how to tweak it in order to be able to make it work.
Moreover, I am aware that in this case I do not have variables, but I tend to use the same style in all of my code.
Here is the code I use currently (which is not working):
$stmt = mysqli_prepare($conn,"SELECT * FROM `assets`");
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
if (!($result = mysqli_stmt_get_result($stmt))){
die("Could not show the required data");}
elseif (empty(mysqli_stmt_num_rows($result))){
echo "Currently there are no assets to be shown.";}
elseif (mysqli_stmt_num_rows($result) > 0){
echo '<table id="table" class="display"><thead>
<tr>
<th>Asset name</th>
<th>Classification</th>
<th>Tag</th>
<th>Department</th>
<th>Next review date</th>
<th>Owner</th>
<th>Update</th>
</tr></thead><tbody>';
while($result2=mysqli_fetch_array($result, MYSQLI_NUM))
{ $class="";
if ($result2["Review_date"] < date('Y-m-d')){$class=" old";} elseif($result2["Review_date"] < date('Y-m-d', strtotime('+6 days'))){$class="notnew";} else {$class="new";}
echo '<tr class="'.$class.'">
<td>'.$result2["Asset_name"].'</td>
<td>'.$result2["Classification"].'</td>
<td>'.$result2["Tag"].'</td>
<td>'.$result2["Department"].'</td>
<td>'.$result2["Review_date"].'</td>
<td>'.$result2["Responsible"].'</td>
<td><a href="editassets.php?id='.$result2['id'].'">Edit</a> | <a href="deleteassets.php?id='.$result2['id'].'" onclick="return confirm(\'Do you want to delete this asset?\');">Delete</a></td>
</tr>';
}
echo '</tbody></table>';
}
I know that my problem is with the first elseif statement, I have tried to use mysqli_stmt_free_result($stmt) but in this case I don't know whether it would be the solution, and if yes, where and how to put it.
Thank you for your help!