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:

scores


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());
}else
{ if($userResult ==FALSE)
{ die("ranking query failed, sorry");
}else
{ if(mysql_num_rows($userResult) ==NULL)
{ die("No ranking results found.");
}else
{ $queryRow = mysql_fetch_row($userResult);
$topIntuition = $query_row['intuition'];
die("queryRow =$queryRow; topIntuition =$topIntuition");
}
}
}


Output:

query row =Array; topIntuition =


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

EDIT:
$query_row[<name of row>]
does not work, but
$query_row[0]
does.

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