0

I have db named CMS with two columns: id and data1

I get following error for the code below when trying to fetch data from the database.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 'table' at line 1' in /var/pdo/index.php:4 Stack trace: #0 /var/pdo/index.php(4): PDO->query('SELECT * FROM t...') #1 {main} thrown in /var/pdo/index.php on line 4

Why am I getting this error?

This is my code:

<?php $db = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'root', 'password'); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $stmt = $db->query('SELECT * FROM table'); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['field1']; } ?> 
2
  • Don't use table as a table name.. Commented Feb 6, 2014 at 14:49
  • Your database is called CMS. What is the table called? Commented Feb 6, 2014 at 16:21

1 Answer 1

7

table is a reserved word. See http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html for the full list.

You could use backticks (SELECT * FROM `table`) if you still want to use your tablename 'table'

Sign up to request clarification or add additional context in comments.

4 Comments

indeed it is: reserved-words
beat me by 5 seconds.
@bart2puck Your answer was better, but because `table` would work.
@kelunik forgot to mention that. I've add it to my answer. Thanks for the addition!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.