mysqli ::准备SQL错误 [英] mysqli :: prepare SQL error

查看:82
本文介绍了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

在此php代码调用中:

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是字符串,可以正确传递.

$tableName is string and is passed correctly.

connect()方法是:

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\n", mysqli_connect_error());
        exit();
    }
}

TIA.

推荐答案

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

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
}

这篇关于mysqli ::准备SQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆