-1

so I'm trying my hand at PDO for a project after referring to w3schools but I keep getting this error

Call to a member function bindParam() on boolean

here's my code. I cannot figure out whats causing it

if (isset($data->title) && isset($data->content) && isset($data->lang) && isset($data->show_on_site)) {
    $response = array();
    try {
        $sql = 'INSERT INTO newsposts
            (heading, content, author, language,show_on_page)
             VALUES (:title, :content,:author,:lang,:show_on_site)';

        $stmt = $db->prepare($sql);
        $stmt->bindParam(':title', $db->real_escape_string($data->title), PDO::PARAM_STR);
        $stmt->bindParam(':content', $db->real_escape_string($data->content), PDO::PARAM_STR);
        $stmt->bindParam(':author', $_SESSION['user_session'], PDO::PARAM_STR);
        $stmt->bindParam(':lang', $db->real_escape_string($data->lang), PDO::PARAM_STR);
        $stmt->bindParam(':show_on_site', $db->real_escape_string($data->show_on_site), PDO::PARAM_BOOL);

        if ($stmt->execute()) {
            header_status(200);
            $response['status'] = 'Success';
            $response['message'] = 'Post Inserted';
        } else {
            header_status(400);
            $response['status'] = 'Error';
            $response['message'] = 'Something went wrong';
        }
        echo json_encode($response);
    } catch (exception $e) {
        header_status(503);
        $response['status'] = 'Error';
        $response['message'] = $e->getMessage();
        echo json_encode($response);
    }
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • If `prepare()` fails it might return `FALSE`. I think it would be the cause, but do not see why would it fail... – Usagi Miyamoto Jul 12 '17 at 08:01
  • Try changing `$stmt->bindParam(':show_on_site', $db->real_escape_string($data->show_on_site), PDO::PARAM_BOOL);` to `$stmt->bindParam(':show_on_site', $db->real_escape_string($data->show_on_site), PDO::PARAM_STR);` – J Shubham Jul 12 '17 at 08:01
  • Where did you define `$db` and Do you have a check like `if($db instanceof PDO)` before prepare ? – mim. Jul 12 '17 at 08:02
  • 1
    Error is related with `$db`. Connection is not made with database, so connection function returned `false`. That is what causing error. – Zedex7 Jul 12 '17 at 08:02
  • @UsagiMiyamoto yes you're right, the `$db->prepare($sql)` part returns false. But why is it happening? –  Jul 12 '17 at 08:04
  • @mim. the $db instance is prepared in a config file which I have required at the top on my page before starting the SQL –  Jul 12 '17 at 08:06
  • @CDsouza check `table name, column name` with database `table names & column names`. `Can not find column name` might be error. use `try catch`. – Zedex7 Jul 12 '17 at 08:08

1 Answers1

0

As documentation states:

If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

so try to set "error mode" in your $db object to "exception" with

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

and wrap prepare() call in "try-catch" block to figure out what's going wrong, like this

try {
    $stmt = $db->prepare($sql);
} catch (PDOException $e) {
    echo $e->getMessage();
}
Nikita U.
  • 3,540
  • 1
  • 28
  • 37