Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
2  
You are aware that LIMIT without ORDER BY rarely makes good sense? –  200_success 3 hours ago
    
I thought that, if not specified, results would have been ordered from the latest inserted to the first one, probably I'm wrong. I'm editing my table adding an unique id and the code. Thanks. –  Angelo 3 hours ago
1  
@Angelo - If you don't specify ORDER 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 an insertedAt timestamp column for that information (which also makes updating erroneous rows easier). –  Clockwork-Muse 1 hour ago
    
Ok, thanks for your advice, I'm gonna do as you said –  Angelo 1 hour ago
add comment

2 Answers

A few things here:

Comments:

Your comments are all compeletely useless.. nothing personal and no offense. That is a beginner-mistake. You don't need to comment everything.

Comments should explain the why and not the how or the what. Exceptions:

  1. License comments
  2. Documentation (python's docstring, java's javadoc, c#'s summary, ...)

Naming:

$con = //...
$query1 = //...
$assoc1 = //...

As opposed to what many people seem to think, source code characters are not costly. Spell it out:

$connection = //...
$userQuery = //...
$userRecord = //...

Formatting:

The convention is, to place subordinate blocks in a visually different place than surrounding code. This is usually done by indenting the block and putting it on a separate starting line

while($assoc1=mysqli_fetch_assoc($query1)){echo $assoc1['user'].'<br>';}

then becomes:

while ($userRecord = mysqli_fetch_assoc($userQuery)) {
    echo $userRecord['user'].'<br>';
}

This would definitely improve the readability of your code, as one line is not a whole loop! Putting too much on one line makes it extremely difficult to follow the thoughts behind your code and makes reading a pain.

share|improve this answer
    
Thank you for your advises, I'll keep them on mind! –  Angelo 3 hours ago
add comment

You want to access both columns in the same loop?

query = mysqli_query($con, "SELECT user, score FROM ris_universita");
while ($assoc = mysqli_fetch_assoc($query) {
    echo $assoc['user'];
    echo $assoc['score'];
}

You could also use "SELECT * FROM ris_universita".

share|improve this answer
    
Thanks, but in the middle of $assoc['user'] and $assoc['score'] I have to insert a string not to be looped –  Angelo 3 hours ago
    
@Angelo and what about it? Why not echo that first? why do you want to keep users and scores separated? –  Vogel612 3 hours ago
    
What do you mean not to be looped? –  hansn 3 hours ago
    
@hansn echo "Users got the following scores:, between the while loops. –  Vogel612 3 hours ago
    
@Vogel612 oh, I get it. Then he will need two loops. –  hansn 3 hours ago
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.