/    Sign up×
Community /Pin to ProfileBookmark

help with order by in mysql

[code=php]<?
global $numc;
$rank6 = 1;
$crenum = mysql_query(“select * from `crew` order by ‘$numc’ desc limit 10 “) or die(mysql_error());
while ($crew1 = mysql_fetch_array($crenum)) {
$numc = mysql_num_rows(mysql_query(“select `crew` from `players` where `crew`=’$crew1[crewid]'”));?>

<?=$rank6?>
<?=$crew1[‘crewid’]?>
<?=$crew1[‘name’]?>
<?=$numc;?>
<?$rank6 += 1;?>
<? } ?>[/code]

ok i’m trying to make it order by the result of $numc(in descending order) but it will not do it

to post a comment
PHP

25 Comments(s)

Copy linkTweet thisAlerts:
@Stephen_PhilbinFeb 20.2005 — By the looks of it, you're trying to use the same variable for two different things and php doesn't know what you want to happen.
Copy linkTweet thisAlerts:
@outrageauthorFeb 20.2005 — hmm what do you suggest for me to do, like what to change so that it orders by that number and it prints out that number
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — can anyone help
Copy linkTweet thisAlerts:
@Stephen_PhilbinFeb 21.2005 — Well first things first. Make sure your variables actually mean something. Did you write this yourself or did you copy and paste it frome somewhere or what? If you just copied and pasted it, then it's not suprising it won't work. If it's is something you wrote yourself, then make sure your variables aren't muddled and they actually carry a value.

Your code looks a tad bonkers to me to be honest. By the looks of it you're declaring a variable to be global when it isn't even in a function and you're breaking your loop prematurely by altering a variable in your SQL query by trying to use it for something else at the same time. that $rank6 variable also appears to be entirely pointless too.

If all you want if for the results to be in a descending order though, all you need is ORDER BY DESC in your SQL query.
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — Quotes are not allowed in the Order By clause. You can code it this way:

$crenum = mysql_query("select * from crew order by {$numc} desc limit 10 ")
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — i wrote this code myself (otherwise it would be working) and no $rank6 isn't pointless its a rankings script so yes it does need it, $numc does carry a variable as mysql_num_rows counts the rows and gives the number of rows of players who have that crew id, and the only reason global $numc; is because it is supposed to carry the variable with the number, order by '$numc' desc isn't it supposed to order it by the num it gets from it come on i don't need advice :-/ i just need it fixed thats all and no you cannot just put ORDER BY DESC alone it will not work i know you are trying to help thanks and all but the code needs to be fixed thats all
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — Did you see my post above?
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — [i]Originally posted by phpnovice [/i]

[B]Quotes are not allowed in the Order By clause. You can code it this way:



$crenum = mysql_query("select * from crew order by {$numc} desc limit 10 ") [/B]
[/QUOTE]


nope it says

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in standings.php on line 147
Copy linkTweet thisAlerts:
@Stephen_PhilbinFeb 21.2005 — [i]Originally posted by outrage [/i]

[B]i wrote this code myself (otherwise it would be working) and no $rank6 isn't pointless its a rankings script so yes it does need it, $numc does carry a variable as mysql_num_rows counts the rows and gives the number of rows of players who have that crew id, and the only reason global $numc; is because it is supposed to carry the variable with the number, order by '$numc' desc isn't it supposed to order it by the num it gets from it come on i don't need advice :-/ i just need it fixed thats all and no you cannot just put ORDER BY DESC alone it will not work i know you are trying to help thanks and all but the code needs to be fixed thats all [/B][/QUOTE]


So basically what you're saying is that you want someone to do it for you.
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — actually no i need help fixing it examples of a code that i would use like phpnovice had shown me to use but you keep on with these advices they are not helping at all actually basically since ppl here know more php than me i can just ask and see what they would use for this but you aren't doing anything but trying to argue, mostly i'm asking for someone to show me what code they would use where in my other posts does it say i want someone to write my codes i need someones help thats what i'm saying, apparently you don't seem to be helping this situation :-/
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — [i]Originally posted by outrage [/i]

[B]nope it says

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in standings.php on line 147 [/B]
[/QUOTE]

[b][i]...and is this what is on line 147?[/i][/b]

while ($crew1 = mysql_fetch_array($crenum)) {

[b][i]For debugging purposes, change this line:[/i][/b]

$crenum = mysql_query("select * from crew order by {$numc} desc limit 10 ")

[b][i]to this:[/i][/b]

$qry = "select * from crew order by {$numc} desc limit 10 ";

echo '<p>qry="{$qry}"</p>';

$crenum = mysql_query($qry)

[b][i]and tell me exactly what kind of output you get.[/i][/b]
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — qry="{$qry}"


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:Inetpubwwwrootoutragestandings.inc.php on line 148

this is what i get maybe its because i don't use the same php and mysql version as yours i use php 4.3.10 and mysql 4.1.9-nt.
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — Oops, sorry, this line:
[code=php]
echo '<p>qry="{$qry}"</p>';
[/code]

should have been like this:
[code=php]
echo '<p>qry="'.$qry.'"</p>';
[/code]
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — qry="select * from crew order by desc limit 10 "

thats what it had shown

woah i never knew that its not recieving variable to do a order by
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — It's receiving the variable -- the variable just has nothing in it. That was my suspicion and was what I wanted to confirm with the [b]echo[/b] of the query string. You can fix it, now, correct? ;-)
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — well i'm still trying things on what to change but if you can tell me what can i change to fix this well shoot it 8-)
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — I'd have to know more about what you're trying to accomplish, and how you've set about doing that, in order to give you any kind of a definitive answer. However, just to get you going, you can hardcode a default column that you'd like to sort on:
[code=php]<?php
global $numc;
$numc = 1; // default order by column
... etc...
[/code]
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — qry="select * from crew order by '1' desc limit 10 "

1. something 1


2. something2 3

this is what i got from putting global $numc; $numc = 1;

well what i'm trying to do is make a rankings script for my homepage and so far everyting works except this and another problem directly related to this but both are just rankings if you need more info just tell me i'm thiking rankings should give you a clue of what i'm doing and the order by in the query is to order the ranks by greatest to least
Copy linkTweet thisAlerts:
@phpnoviceFeb 21.2005 — I see you've got quotes back in your order by clause. You'll have to remove those.
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — yes just did same results
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — ok to explain what i'm trying to do here is that in my code $crenum is supposed to select the crewids then $numc comes in effect by selecting all the of the players by crew like if 2 people have #1 for crew then it puts 2 people as crewid #1 then if 3 people are in crew #2 then its 3 people inside that crew then i wanted it to order the crews by their added people state thats why its order by $numc desc but it isn't working because it saying that the variable has nothing thats why i tried using global to see if that would help but nada nothing still, its basically rankings my code isn't efficent enough maybe someone could change my code around
Copy linkTweet thisAlerts:
@outrageauthorFeb 21.2005 — example:

  • 1. crew 10 members

  • 2. crew1 9 members

  • 3. crew2 8 members

  • 4. crew3 7 members

  • 5. crew4 6 members

  • 6. crew5 5 members

  • 7. crew6 4 members

  • 8. crew7 3 members

  • 9. crew8 2 members

  • 10. crew9 1 members


  • that is an example of what my code should be doing
    Copy linkTweet thisAlerts:
    @phpnoviceFeb 21.2005 — The problem seems too big to be able to fix it in these forums. The only thing I can suggest is what I would do if I were in your situation. I would start puting in ECHO statements to find out where the process is breaking down. Does this code execute in a loop?
    Copy linkTweet thisAlerts:
    @outrageauthorFeb 21.2005 — well forget it i solved it with something like this select crewid,name,count(*) as totalmembers from crew join players on crewid=crew group by crewid order by totalmembers desc limit 10
    Copy linkTweet thisAlerts:
    @phpnoviceFeb 22.2005 — As long as you're happy... ;-)
    ×

    Success!

    Help @outrage spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...