简体   繁体   中英

What's wrong with my PDO/MySQL syntax?

I'm using the following code:

$query = $dbh->prepare("INSERT into crm_order_errors "
                      ."SET (order_id, number_of_attempts, last_attempt) "
                      ."VALUES (:order_id, 0, :last_attempt) "
                      ."ON DUPLICATE KEY UPDATE number_of_attempts = number_of_attempts + 1, last_attempt = :last_attempt"
         );

$query->execute(array(':order_id'=>$orderId, ':last_attempt'=>1332849904);

This produces the following error:

PHP Warning: PDOStatement::execute(): 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 '(order_id, number_of_attempts, last_attempt) VALUES ('10297', 0, '' at line 1 in /_myClasses/MYSQL_Logger.php on line 57

It simply isn't clear to me where the error is. It appears that the value for :last_attempt is not getting found, since the warning puts its value at '' : Values ('10297', 0, '' . Why is that happening, and is that the source of the problem?

Also, am I allowed to use the same placeholder twice in a prepared statement (in this case, :last_attempt ).

You are missing an end parenthesis on your execute function.

Should be:

$query->execute(array(':order_id'=>$orderId, ':last_attempt'=>1332849904));

And you need to remove the SET keyword. It's for UPDATE statements, not INSERT statements.

您不需要SQL语句中的SET

You are missing a parenthesis:

$query->execute(array('order_id' => $orderId, 'last_attempt' => 1332849904));

Also remove SET from your INSERT INTO statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM