Search code examples
sqlprepared-statementsql-insertbindparam

Error in php sql statement ($stmt2 is returning false but I dont know why)


I am getting this error (Call to a member function bind_param() on a non-object ) for my prepared statement as below which I know means that there is some error in the sql statement but I cannot see any error at all. I have been staring at this for a while now and I cannot see the error, I also tried to echo the sql statement and it seemed to be good. Could someone please tell me where I have gone wrong here?

    $sql2 =
        "INSERT INTO $this->table_1 ($this->key_1, $this->key_2, $this->key_3, $this->key_4, $this->key_5)
         VALUES (?, ?, ?, ?, ?);";

    $stmt2 = $this->connection->prepare($sql2);
    $stmt2->bind_param("isssi", $1, $2, $3, $4, $5);
    $stmt2->execute();
    $stmt2->close();

Thanks


Solution

  • Finally found my problem, turns out my table name "keys" was a reserved word and to escape it I had to use backslashes like this "``keys`" (ignore the second backtick before the word, SO turns this into a code block if there is only one backtick annoyingly). If anyone in the future runs into this problem and you are sure that your code is correct try and escape your column names or table names