mysqli ::准备SQL错误
问题描述:
Help please :). I'm gettig this error:
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
on this php code call:
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 is string and is passed correctly.
connect() method is:
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
", mysqli_connect_error());
exit();
}
}
TIA.
请帮助:)。 我发现这个错误: p>
警告:mysqli :: prepare()[mysqli.prepare] :( 42000/1064):你的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在*** / classes / db.mysql.class.php的第1行'?(id INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(id))'附近使用正确的语法 在第69行
警告:mysqli :: prepare()[mysqli.prepare] :( 42000/1064):您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册,以便在第75行的*** / classes / db.mysql.class.php第1行“?”附近使用正确的语法
code> pre >
关于这个php代码调用: p>
public function createTable($ tableName){
$ this-> connect(); \ n
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();
}
code> pre>
$ tableName是字符串并传递 正确。 p>
connect()方法是: p>
private function connect(){
$ this-> dbSocket = new mysqli ($ this-> dbHost,$ this-> dbUser,$ thi s-> dbPassword,$ this-> dbDatabase);
if(mysqli_connect_errno()){
printf(“Brakpołączeniazserwerem MySQL。 Kodbłędu:%s
“,mysqli_connect_error());
exit();
}
}
code> pre>
TIA。 p> \ n div>
答
You cannot use a table name as a parameter.
If the point of this is to create several tables with the same structure but different name, I suggest using something like:
$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
}