tags:

views:

56

answers:

2

Hi, I'm making a simple cms system for a site I'm making for non-tech users to edit...

So far so good but when I try and run this code I keep getting: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''pages' ORDER BY 'pages'.'pageOrder' ASC LIMIT 0 , 30' at line 1

By the error it looks like a problem with the order by section and indeed it works without it...

$sql = "SELECT * FROM 'pages' ORDER BY 'pages'.'pageOrder' ASC LIMIT 0 , 30";

$result = mysql_query($sql) or die(mysql_error());

Now I know there is nothing wrong with the code because originally I wrote my own SQL but then after it failed I robbed some from phpmyadmin and it still gives the error but it works in phpmyadmin...

I'm really at my wits end with this, help is very much appreciated thank you...

+6  A: 

You shouldn't write 'pages'. Use backticks instead of single quotes for table and column names. Single quotes are used only for strings.

And backticks aren't necessary here anyway. Backticks are generally only required for names that are reserved words in SQL, and names containing special characters or spaces. So you could just do this:

SELECT * FROM pages ORDER BY pageOrder LIMIT 30
Mark Byers
Well originally I didn't, that SQL is just what I took straight out of phpmyadmin but this doesn't solve my problem...
theflyinghaiwian
Wait I stand corrected, it did solve it - I don't understand why but thank you good sir :)
theflyinghaiwian
@theflyinghaiwian - because single quotes (') are for strings only and calling a 'table' as a string is a syntaxes error.
Ben
+2  A: 

The quotes in your query are incorrect. You could either use

$sql = "SELECT * FROM `pages` ORDER BY `pages`.`pageOrder`  ASC LIMIT 0 , 30";

if you really need to fully qualify the table/column, or just leave that out and use

$sql = "SELECT * FROM pages ORDER BY pageOrder ASC LIMIT 0 , 30";
hangy