If I have to loop results of a query echoing first all fields of a column, then echoing something not to loop, then fields of another column. So I'm using
<?php
$con = mysqli_connect();
echo 'Latest users who tried our test<br>';
//select the fields of the first column
$query1 = mysqli_query($con,"SELECT user FROM ris_uinversita LIMIT 0,10");
//loop the results
while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['user'].'<br>';}
//echo something not to loop
echo 'Users got the following scores<br>';
//select the fields of the second column
$query2 = mysqli_query($con,"SELECT score FROM ris_universita LIMIT 0,10");
//loop the results
while($assoc2=mysqli_fetch_assoc($query2)){echo $assoc2['score'].'<br>';}
?>
Is it possible to use just one query like this
mysqli_query($con,"SELECT * FROM ris_universita LIMIT 0,10");
To select both fields just once? If it could be helpful, table "ris_universita" has just the two columns "score" (tinyint) and "user" (varchar 30), for now there aren't so many records, but I hope in future there will.
LIMIT
withoutORDER BY
rarely makes good sense? – 200_success♦ 3 hours agoORDER BY
, the optimizer will return rows in whatever order it feels like. If either one of those columns starts an index, it's probably going to start with that. A pair of queries is unlikely to match users and scores. Don't rely on auto-generated ids for insertion order, especially for concurrent systems and anything with triggers: they can only be relied upon to give you a unique value. Use aninsertedAt
timestamp column for that information (which also makes updating erroneous rows easier). – Clockwork-Muse 1 hour ago