MySQL order by mistake in PHP

I create a simple cms system for a site that I create for non-technical users to edit ...

So far so good, but when I try to run this code, I still get: you have an error in the SQL syntax; check the manual that matches the version of MySQL server for the correct syntax for using the "ORDER BY" "Pages" pages. "pageOrder" ASC LIMIT 0, 30 "on line 1

By mistake, this looks like a problem with section order and really 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 that there is nothing wrong with the code, because I originally wrote my own SQL, but then, after its failure, I robbed some of phpmyadmin, and it still gives an error, but it works in phpmyadmin ...

I am really on my way with this, help is much appreciated, thanks ...

+3
source share
2 answers

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

And backlinks are not needed here. Backticks are usually only required for names that are reserved words in SQL, and names that contain special characters or spaces. So you can just do this:

SELECT * FROM pages ORDER BY pageOrder LIMIT 30
+7
source

The quotation marks in your request are incorrect. you can 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 it and use

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

Source: https://habr.com/ru/post/1742605/


All Articles