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
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>";
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.