ORDER BY...ASC

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

ORDER BY...ASC

Post by Ghoz »

Hi,

Please help to check this code. Is it correct???

Code: Select all

$result = mysql_query("SELECT Name from users WHERE Id='".$User_Id."'ORDER BY Name ASC");
Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: ORDER BY...ASC

Post by requinix »

Syntactically? Logically? Metaphysically? Extemporaneously? Are you having a problem or just throwing this question out there to see what you get back?
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: ORDER BY...ASC

Post by tr0gd0rr »

- It is weird without a space between the second apostrophe and "ORDER" but it works.
- You should escape $User_Id or cast it to an integer.
- It seems like the query would only ever return one result so the ORDER BY would have no effect.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY...ASC

Post by Celauran »

Stop using mysql_query. Stop using mysql_ anything.

MySQLi
PDO
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: ORDER BY...ASC

Post by Ghoz »

The code doesn't seems to work. Perhaps something wrong with the code or my other codes.
Thanks to tr0gd0rr, I will check your suggestions.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: ORDER BY...ASC

Post by Celauran »

"It doesn't work" is not helpful. What errors are you getting?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: ORDER BY...ASC

Post by Christopher »

It might not work for two reasons: 1) there are no records matching the value of $User_Id, 2) you have a syntax error in your SQL:

Code: Select all

$result = mysql_query("SELECT Name from users WHERE Id='".$User_Id."'ORDER BY Name ASC");
if ($result === false) {
    echo 'Error: ' . mysql_error();
} elseif (mysql_num_rows($result) == 0) {
    echo 'No rows returned. ';
}
And, don't use the mysql_* functions -- use PDO.
(#10850)
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: ORDER BY...ASC

Post by Ghoz »

Hi,
The code did give me the result but not in ascending order. Then I tried the code below and it didn't echo error or 'No rows returned'

Code: Select all

$result = mysql_query("SELECT Name from users WHERE Id='".$User_Id."'ORDER BY Name ASC");
if ($result === false) {
    echo 'Error: ' . mysql_error();
} elseif (mysql_num_rows($result) == 0) {
    echo 'No rows returned. ';
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: ORDER BY...ASC

Post by Christopher »

Did you try adding a space before orderby : $User_Id."' ORDER BY
(#10850)
Ghoz
Forum Commoner
Posts: 38
Joined: Thu Jul 12, 2012 2:32 am

Re: ORDER BY...ASC

Post by Ghoz »

Yes I did
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: ORDER BY...ASC

Post by Christopher »

What order are they in? What does the data look like? What kind of field is Name?
(#10850)
Post Reply