Jump to content

[SOLVED] MySql Order By


zackcez

Recommended Posts

I keep getting an error when I try this so...Here's my current codes:

$total = Attackxp + Woodcuttingxp + Defencexp + Strengthxp + Hitpointsxp + Rangexp + Prayerxp + Magicxp + Cookingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftingxp;

$result = mysql_query("SELECT * FROM user ORDER BY $total DESC LIMIT 25");

If anyone knows the correct way to do this...please post here ;o

BTW, Attackxp, ect are all rows in the user table ;)

Link to comment
https://forums.phpfreaks.com/topic/113142-solved-mysql-order-by/
Share on other sites

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [path]/modules/test.php on line 99

Here's the fetch array...but I already know the problem isn't in that:

while($row = mysql_fetch_array($result)){
$i++;
echo "<tr align=\"center\"><td class=\"alt1\" nowrap=\"nowrap\" width=\"5%\" align=\"left\">$i</td><td class=\"alt1\" nowrap=\"nowrap\" width=\"15%\" align=\"left\">" . $row['username'] . "</td> <td class=\"alt1\" nowrap=\"nowrap\" width=\"10%\" align=\"left\">" . $row[$skill_lvl] . "$total</td><td class=\"alt1\" nowrap=\"nowrap\" width=\"15%\" align=\"left\">" . $row[$stat] . "$totalxp</td></tr>";
}

The syntax for your query ought to be SELECT from tablename ORDER by fieldname DESC LIMIT 25.  If the value of $total happened to be 250, do you actually a database field named 250? I doubt it. And that's why you get an error.

Not so, he is just missing string quotes around the field names, MySQL can do the summation work for him.

 

$total = "Attackxp + Woodcuttingxp + Defencexp + Strengthxp + Hitpointsxp + Rangexp + Prayerxp + Magicxp + Cookingxp + Fletchingxp + Fishingxp + Firemakingxp + Craftingxp + Smithingxp + Miningxp + Herblorexp + Agilityxp + Thievingxp + Slayerxp + Farmingxp + Runecraftingxp";

$q = "SELECT * FROM user ORDER BY $total DESC LIMIT 25";
echo $q;
$result = mysql_query($q);

 

The reason I put the query into its own variable $q and made it echo is so you can see what the actual query you're running is. You can then plug that into a DB front-end (like PhpMyAdmin) to see what the result set is if need be.

 

Also, you should check out http://php.net/mysql_error

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.