SQLSTATE [42000]:语法错误或访问冲突:

时间:2014-04-13 08:14:08

标签: php mysql

我一直在尝试修复过去30分钟内的SQL状态错误。 我在这做错了什么? 欢迎任何建议。

$statement=$conn->prepare("SELECT to.ipaddress as ipaddress, to.email as email, to.orderdate as orderdate, to.is_completed as is_completed, p.price as price from temporder to, products p WHERE to.productid = p.productid AND to.ipaddress=? AND to.orderdate=? ");
$statement->execute(array(
$_SERVER['REMOTE_ADDR'],
date('Y-m-d')
));
$statement->setFetchMode(PDO::FETCH_ASSOC);

完成错误

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在正确的语法附近使用,产品p WHERE to.productid = p.productid AND to.ipaddress =' :: 1'和to.or'在第1行

2 个答案:

答案 0 :(得分:4)

TO(你用作ALIAS)在MySQL中是reserved word,你需要在反引号中包围它。

从查询的一部分......

e as price from temporder to, products p WHERE to
                         ^^^^ //<----- That's a reserved word. !

将它们包裹在下方的反击下..

e as price from temporder `to`, products p WHERE to

答案 1 :(得分:2)

to是保留关键字

所以你需要将它包装在反引号中

`to`

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