Page 1 of 1

ORDER BY...ASC

Posted: Wed Oct 17, 2012 4:27 am
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

Re: ORDER BY...ASC

Posted: Wed Oct 17, 2012 4:55 am
by requinix
Syntactically? Logically? Metaphysically? Extemporaneously? Are you having a problem or just throwing this question out there to see what you get back?

Re: ORDER BY...ASC

Posted: Wed Oct 17, 2012 1:47 pm
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.

Re: ORDER BY...ASC

Posted: Wed Oct 17, 2012 7:11 pm
by Celauran
Stop using mysql_query. Stop using mysql_ anything.

MySQLi
PDO

Re: ORDER BY...ASC

Posted: Wed Oct 17, 2012 10:26 pm
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.

Re: ORDER BY...ASC

Posted: Thu Oct 18, 2012 5:08 am
by Celauran
"It doesn't work" is not helpful. What errors are you getting?

Re: ORDER BY...ASC

Posted: Thu Oct 18, 2012 11:21 am
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.

Re: ORDER BY...ASC

Posted: Sat Oct 20, 2012 10:50 pm
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. ';
}

Re: ORDER BY...ASC

Posted: Sun Oct 21, 2012 11:02 am
by Christopher
Did you try adding a space before orderby : $User_Id."' ORDER BY

Re: ORDER BY...ASC

Posted: Mon Oct 22, 2012 3:51 am
by Ghoz
Yes I did

Re: ORDER BY...ASC

Posted: Mon Oct 22, 2012 12:48 pm
by Christopher
What order are they in? What does the data look like? What kind of field is Name?