views:

29

answers:

1

Hello!

I am new here. The code and error below I try to solve in three days. Could anyone help me?

$db = new PDO('mysql:dbname=' . DBNAME . ';host=' . DBHOST, DBUSER, DBPASS, array(PDO::ATTR_PERSISTENT => true));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("INSERT INTO A (phone, city, address) VALUES ('1234567890', 'big city', 'somwhere')");
$db->exec("INSERT INTO B (product, price, email) VALUES ('apple', 50, 'email@localhost')");

The 1st exec is ok but 2nd exec(INSERT INTO B) always get

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 'B (product, price, email) VALUES' at line 1

But this statement can execute successfully using MySQL Query Browser. My PHP is 5.3.0 and MySQL is 5.1.30-community.

Thank you!

A: 

The PDO/MySQL driver has some wacky bugs that haven't been fixed in over 2 years, and some of these result in Error #1064. (Search bugs.php.net for more disgusting details.)

It seems that you don't have any errors in your SQL, so make sure you have insert privileges to both tables, and try running the query in several different ways.

  • Using query() instead of exec()
  • Using prepared statements with bound parameters
  • Try putting quotes around 50
kijin