"Undefined method PDO::execute()" despite using prepare

115 views Asked by At

This code has been working before, but I recently added a database class. I get the instance and connection from it:

           $connection = MYSQLDatabase::getInstance()->getConnection();
            $connection->prepare("INSERT INTO users etc etc.......
            $insertArray = $connection->execute(array(
                ":username" => $_POST["username"]
            ));

getInstance() returns the database instance. getConnection() returns the connection property which contains:

new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);

So the error occurs when doing $connection->execute despite that $connection contains the database object, the connection, and the prepared statement. How can this be?

1

There are 1 answers

1
xcvd On BEST ANSWER

Create a variable to store your prepared statement then execute that.

$connection = MYSQLDatabase::getInstance()->getConnection();
$statement = $connection->prepare("INSERT INTO users etc etc.......");
$insertArray = $statement->execute(array(
    ":username" => $_POST["username"]
));