Reputation: 3
I have a table called pvpstats. It has 4 columns; id, name, kills and deaths. I have to get 3 highest values from "kills" and sort them from highest to lowest and include also the name. Like 3-top statistics! And because I'm new with MySQL I need an step-by-step quide...
This is how my .php looks at the moment:
<?php
$server = "localhost";
$dbuser = "usernm";
$dbpass = "passwd";
$dbname = "pvpstats";
mysql_connect($server, $dbuser, $dbpass);
mysql_select_db($dbname);
$result = mysql_query("SELECT kills FROM pvpstats ORDER BY kills DESC LIMIT 3");
$row = mysql_fetch_assoc($result);
echo print_r($row,true);
?>
Connection is working and it is showing the highest value but without name. And only the highest when it should show 3 highest
Upvotes: 0
Views: 140
Reputation: 567
$row = mysql_fetch_assoc($result);
will only echo 1 row
user while loop to echo all data.
Upvotes: 1
Reputation: 4111
it seems,your query syntax does not have any problem. use below codes to find problems.
mysql_connect($server, $dbuser, $dbpass) or die(mysql_error());
...
$row = mysql_fetch_assoc($result) or die(mysql_error());
Upvotes: -1
Reputation: 359
Well for one, you should use:
print_r($row);
not:
echo print_r($row,true);
*print_r* will automatically output it if you omit the true variable, as passing the response to echo is just redundant.
Also, since your query will return three rows, you have to loop through it:
while ($row = mysql_fetch_assoc($result)) {
print_r($row);
// or if you just want to output the number, use:
// echo $row["kills"];
}
Upvotes: 1