-1

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 FATAL ERROR 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!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Tino Caer
  • 443
  • 4
  • 19

3 Answers3

3

Instead of quotes use back-tics:-

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

Or

$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.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
2

Although you should use mysqli or pdo as mysql is removed from php 7, however use backtick instead of single quotes arround table name

 $id = 1;

 $query =  mysql_query("SELECT * FROM `Articles` WHERE ID = '$id'") or die("FATAL ERROR");
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
2
Change 'Articles' on `Articles`

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

if Articles.ID is integer,use this

$query =  mysql_query("SELECT * FROM `Articles` WHERE `ID` = ".$id) or die("FATAL ERROR");
Vanya Avchyan
  • 880
  • 7
  • 24