Search code examples
phpmysqlsqlcreate-tableprepare

MySQL -- Fatal error: Call to a member function bind_param() on a non-object


I've searched through the dozens of questions with near identical title to this post, but I have yet to find answer to my specific problem. I'm trying to prepare a statement, but I get this error every time. I'm thinking there must me some sort of error on my sql syntax, but for the life of me I cannot find it.

$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();

Solution

  • You can't use a placeholder for a table name, it can only be used in place of expressions. So CREATE TABLE ? is not valid. Use:

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

    after validating that $title is an acceptable table name.