Bader H Al Rayyes
Bader H Al Rayyes

Reputation: 522

mysql Erorr when use select ?

i have a problem , each time i try to do select and fetch this error show

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 'add' at line 1

  <?php
      mysql_connect("localhost", "root", "123") or die() ; 
 mysql_select_db("boom") or die() ; 
 //Retrieves data from MySQL 
 $select= "select * from add" ;
 $data = mysql_query($select) or die(); 
 //Puts it into an array 
 while($info = mysql_fetch_array( $data )) 
 { 

 echo "<img src=images/".$info['photo'] .">";
 }
 //Outputs the image and other data
 ?>

what is the problem here ?

Upvotes: 2

Views: 98

Answers (4)

Ignacio
Ignacio

Reputation: 5730

$select = "select * from `add` ;

Adding the backticks will solve your problem. ADD is a reserved word in MySQL

Upvotes: 3

mandreko
mandreko

Reputation: 1796

"Add" is a reserved word in MySQL. You can either change the name of your table, or escape it out. According to this it looks like you would need to put backticks around the table name, like add.

Upvotes: 1

Johan
Johan

Reputation: 76670

add is a reserved word:

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Rewrite the query like so:

$select= "select * from `add`" ; 

Upvotes: 4

Darbio
Darbio

Reputation: 11418

Ensure that 'add' is the table name (add is a reserved word), and try escaping your table name with the ` character.

Backticks are good practise to escape the table name for MySQL (Next to the 1 on your keyboard).

SELECT * FROM `add`

Upvotes: 2

Related Questions