PDA

View Full Version : mysql - sorting and calling help


Tekton
08-24-2004, 08:08 AM
Basically what I want to do is get the top 7 people with the most 'gold'.

This gives me the top person, but I need a way to get all 7. Any help? Thanks.

$temp = $DB_site->query_first("SELECT username, gold FROM ".TABLE_PREFIX."user ORDER BY gold DESC LIMIT 7 ");
$top_one = $temp[username];
$top_one2 = $temp[gold];

CarCdr
08-24-2004, 09:38 AM
Use query rather than query_first, then loop over the results The class DB_site offers the function query_first as a shortcut for a) regular query, b) free mysql result storage, c) return the single row.

Colin F
08-24-2004, 09:50 AM
Basically what I want to do is get the top 7 people with the most 'gold'.

This gives me the top person, but I need a way to get all 7. Any help? Thanks.

$temp = $DB_site->query_first("SELECT username, gold FROM ".TABLE_PREFIX."user ORDER BY gold DESC LIMIT 7 ");
$top_one = $temp[username];
$top_one2 = $temp[gold];

You might also make that "LIMIT 0, 6"

CarCdr
08-24-2004, 10:23 AM
You might also make that "LIMIT 0, 6"
What would that achieve Colin? That would return the first 6 rows. LIMIT 0,6 is equivalent to LIMIT 6.

Colin F
08-24-2004, 11:49 AM
What would that achieve Colin? That would return the first 6 rows. LIMIT 0,6 is equivalent to LIMIT 6.
really?

I would have thought LIMIT 0,6 would return rows 0 through 6, making the first seven rows. Good to have learned something :)

Dean C
08-24-2004, 12:13 PM
Yes Colin, I'm pretty sure what you said is right. LIMIT 0,6 would return the first 7 seven rows.

Tekton
08-24-2004, 04:23 PM
Can I get a quick example on how to loop the results, and store them each in SEPERATE variables? Thanks. ^__^;;

Colin F
08-24-2004, 04:36 PM
$winners = $DB_site->query("SELECT username, gold FROM ".TABLE_PREFIX."user ORDER BY gold DESC LIMIT 7 ");
while ($winner = $DB_site->fetch_array($winners))
{
$id++;
$uname = "name_".$id;
$ugold = "gold_".$id;
${$uname} = $winner[username];
${$ugold} = $winner[gold];
}



That should work. you can then use the variables $name_1 - $name_7 as well as $gold_1 - $gold_7

Tekton
08-24-2004, 04:42 PM
Hey, wow! Thanks a lot! I'm prolly trying to do something too big as my first home-made hack of sorts, but this is one problem out of the way. Thanks again! :)

CarCdr
08-24-2004, 09:04 PM
Yes Colin, I'm pretty sure what you said is right. LIMIT 0,6 would return the first 7 seven rows.
It is as I said...

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must be integer constants. With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.