Chris Tolworthy Chris Tolworthy -4 years ago 29
MySQL Question

mysql_fetch_row and ORDER BY - what am I doing wrong?

Sorry for the vague title, but I've been trying to nail this for a week and have run out of ideas.

Table: name:


id name password intuition
(int, varchar, varchar, int)
(5 rows with made-up values)

The PHP:

$userResult = mysql_query("SELECT `intuition` FROM `scores` ORDER BY `intuition` DESC LIMIT 4,1");
if($userResult ==NULL)
{ die(mysql_error());
{ if($userResult ==FALSE)
{ die("ranking query failed, sorry");
{ if(mysql_num_rows($userResult) ==NULL)
{ die("No ranking results found.");
{ $queryRow = mysql_fetch_row($userResult);
$topIntuition = $query_row['intuition'];
die("queryRow =$queryRow; topIntuition =$topIntuition");


query row =Array; topIntuition =

should be the fifth highest result, currently the integer 2. What am I doing wrong?

$query_row[<name of row>]
does not work, but

Answer Source

LIMIT 4, 1 will return the fifth highest result (as LIMIT 0, 1 returns the first).

Regardless, mysql_fetch_row returns an array with keys that are numeric - try $queryRow[0];

Alternatively, you can switch to mysql_fetch_array or mysql_fetch_assoc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download