0

请帮忙 :)。我得到这个错误:

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

在这个 php 代码调用中:

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。

4

1 回答 1

2

您不能使用表名作为参数。

如果这样做的目的是创建几个具有相同结构但名称不同的表,我建议使用类似的东西:

$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
}
于 2012-07-19T11:09:38.280 回答