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
Ghoz
Forum Commoner
Posts: 38 Joined: Thu Jul 12, 2012 2:32 am
Post
by Ghoz » Wed Oct 17, 2012 4:27 am
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
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Oct 17, 2012 4:55 am
Syntactically? Logically? Metaphysically? Extemporaneously? Are you having a problem or just throwing this question out there to see what you get back?
tr0gd0rr
Forum Contributor
Posts: 305 Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA
Post
by tr0gd0rr » Wed Oct 17, 2012 1:47 pm
- 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.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Wed Oct 17, 2012 7:11 pm
Stop using mysql_query. Stop using mysql_ anything.
MySQLi
PDO
Ghoz
Forum Commoner
Posts: 38 Joined: Thu Jul 12, 2012 2:32 am
Post
by Ghoz » Wed Oct 17, 2012 10:26 pm
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.
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Thu Oct 18, 2012 5:08 am
"It doesn't work" is not helpful. What errors are you getting?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Oct 18, 2012 11:21 am
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
Post
by Ghoz » Sat Oct 20, 2012 10:50 pm
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. ';
}
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Sun Oct 21, 2012 11:02 am
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
Post
by Ghoz » Mon Oct 22, 2012 3:51 am
Yes I did
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Oct 22, 2012 12:48 pm
What order are they in? What does the data look like? What kind of field is Name?
(#10850)