代码之家  ›  专栏  ›  技术社区  ›  xcdemon05

MySQL--致命错误:调用非对象上的成员函数bind_param()

  •  1
  • xcdemon05  · 技术社区  · 10 年前

    我已经搜索了几十个与本文标题几乎相同的问题,但我还没有找到具体问题的答案。我正在准备一份声明,但每次都会出错。我在想我的sql语法一定有什么错误,但我一辈子都找不到。

    $title = "this_is_my_table_title";    
    
    //import the database login credential variables
    require("localhost_credentials.php");
    
    $conn = new mysqli($db_servername, $db_username, $db_password, $db_name);
    if($conn->connect_error)
    {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $querystr  = "CREATE TABLE ? (";
    $querystr .= "id int(11) not null auto_increment,";
    $querystr .= "section varchar(255) not null,";
    $querystr .= "raw_content LONGTEXT not null,";
    $querystr .= "formatted_content LONGTEXT not null,";
    $querystr .= "primary key (id)";
    $querystr .= ");";
    
    
    $statement = $conn->prepare($querystr);  <--- error here
    $statement->bind_param("s", $title);
    $statement->execute();
    $statement->close();
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   Barmar    10 年前

    不能为表名使用占位符,它只能用于代替表达式。所以 CREATE TABLE ? 无效。使用:

    $querystr  = "CREATE TABLE `$title` (";
    

    验证后 $title 是可接受的表名。