mysqli ::准备SQL错误
请帮忙:)。我gettig这个错误:这个PHP代码调用mysqli ::准备SQL错误
Warning: mysqli::prepare() [mysqli.prepare]: (42000/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 '?(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))' at line 1 in ***/classes/db.mysql.class.php on line 69
Warning: mysqli::prepare() [mysqli.prepare]: (42000/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 '?)' at line 1 in ***/classes/db.mysql.class.php on line 75
:
public function createTable($tableName) {
$this->connect();
if ($stmt = $this->dbSocket->prepare("CREATE TABLE ?(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))")) {
$stmt->bind_param("s", $tableName);
$stmt->execute();
$stmt->close();
}
if ($stmt = $this->dbSocket->prepare("INSERT INTO sys_userTables(userTableName) VALUES (u_?)")) {
$stmt->bind_param("s", $tableName);
$stmt->execute();
$stmt->close();
}
$this->disonnect();
}
$ tablename是字符串,并正确地传递。
connect()方法是:
private function connect() {
$this->dbSocket = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbDatabase);
if (mysqli_connect_errno()) {
printf("Brak połączenia z serwerem MySQL. Kod błędu: %s\n", mysqli_connect_error());
exit();
}
}
TIA。
不能使用表名作为参数。
如果这点是创建具有相同的结构,但不同的名称几个表,我建议使用类似:
$table_names = array('a', 'b', 'c');
foreach($table_names as $name) {
$query = "CREATE TABLE `$name` (id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))";
// run query or add it to a collection to run later
// or append a ';' to the end of the string and do it with a multi_query
}
不要忘记转义表名,因为mysqli驱动程序不会转义插入的变量。 – 2012-07-19 11:12:31
我忽略了它!谢谢:) – 2012-07-19 11:15:28
谢谢,这是问题。 – grasnal 2012-07-19 12:53:18
的【什么是错的SQL?](DUP HTTP:// *.com/q/8215433/),[我可以在准备好的语句中参数化表名?](http://*.com/q/11312737/) – outis 2012-07-19 10:50:59
您不能绑定表名,只有参数。 – 2012-07-19 10:51:18