Tino Caer Tino Caer - 1 year ago 76
SQL Question

mysql_select not working in PHP

So I am trying to select rows from a table called articles based on the ID. For some reason, I am having a problem with this and I am not sure why. The exact problem is that the script echoes a

which means that my query is not working.

I updated the script to echo the error and here it is:

You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ''Articles' WHERE 1' at line 1

Here is the code:

$id = 1;

$query = mysql_query("SELECT * FROM 'Articles' WHERE ID = '$id'") or die("FATAL ERROR");

I tried just a simple select statement and that didn't work either:

$query = mysql_query("SELECT * FROM 'Articles' WHERE 1") or die("FATAL ERROR");

Help is much appreciated, thanks!

Answer Source

Instead of quotes use back-tics:-

$query =  mysql_query("SELECT * FROM `Articles` WHERE `ID` = $id") or die("FATAL ERROR");


$query =  mysql_query("SELECT * FROM `Articles` WHERE 1") or die("FATAL ERROR");

Note:- Start using mysqli_* OR PDO instead of mysql_*, because Mysql_* functions are removed in php 7.0 and was already deprecated in php 5.5

Also check column name ID is correct or not, because usually we use id.