0

I'm trying to insert information insto a mySQL database and I get the WSOD and this error:

PHP Fatal error: Call to a member function bindParam() on a non-object ...

This is the code:

try {
        $conectar1 = new PDO('mysql:host='.HOST.'; dbname='.DATABASE.'; charset=utf8', USER, PASS); 

    $guardarPost = $conectar1->query("
        INSERT INTO foro 
        (userID, estadoPost, asuntoPost, postUltimo, datosPost)
        VALUES (?, ?, ?, ?, ?)
    ");
    $guardarPost->bindParam(1, $userID); <============ ERROR LINE
    $guardarPost->bindParam(2, $estadoMensaje);
    $guardarPost->bindParam(3, $asuntoPost);
    $guardarPost->bindParam(4, $fechaMensaje);
    $guardarPost->bindParam(5, $datosPost);
    $ok = $guardarPost->execute();
} catch (PDOException $e) {
    echo "Error ".$e->getMessage();
}

I've tried stripping everything to check is the DB connection work, and it does.

I've tried entering the query manually into phpMyAdmin replacing the question marks for the text, and it does work.

Where's the problem?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rosamunda
  • 14,620
  • 10
  • 40
  • 70
  • 3
    You need to use `->prepare()` not `->query()` to use `->bindParam()`. [`PDO::query() executes an SQL statement in a single function call`](http://php.net/manual/en/pdo.query.php) vs [`PDO::prepare Prepares a statement for execution and returns a statement object`](http://php.net/manual/en/pdo.prepare.php) – Sean Feb 19 '17 at 21:42

1 Answers1

1

Try print_r($conectar1->errorInfo());

also use prepare() instead of query() to use bindParam()s

Albert221
  • 5,223
  • 5
  • 25
  • 41
Abdul Rafay
  • 312
  • 6
  • 17
  • That was it: I forgot to use prepare instead of query! Thanks! I'll accept the answer in a few minutes, when it let me do it. :) – Rosamunda Feb 19 '17 at 21:48