PDO执行int值的错误

时间:2011-03-19 04:12:08

标签: php pdo

我正在尝试使用PDO exec保存UNIX时间戳,但是我遇到了访问冲突:

check the manual that corresponds to your MySQL server version for the
     

在'1300507025'附近使用正确的语法

当我使用字符串时,exec按预期工作。我需要做些什么来使用整数值?

这是声明

exec( "CREATE DATABASE $dbName" );

2 个答案:

答案 0 :(得分:2)

这是一个非常糟糕的主意,但您需要使用反引号来创建数据库:

mysql> create database 123;
ERROR 1064 (42000): 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 '123' at line 1
mysql> create database "123";
ERROR 1064 (42000): 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 '"123"' at line 1
mysql> create database '123';
ERROR 1064 (42000): 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 ''123'' at line 1
mysql> create database `123`;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| 123                |
...

mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.0.77    |
+-----------+
1 row in set (0.00 sec)

答案 1 :(得分:0)

exec( "CREATE DATABASE \'$dbName\'" );只引用数据库名称。

编辑:

这只适用于你已经:

SET sql_mode='ANSI_QUOTES'; 

否则使用转义后退刻度标记