1

I am learning to work with PHP and have a simple problem.

<?php
   ini_set('display_errors', 'On');
  error_reporting(E_ALL);
  $db = new PDO('sqlite:/usr/users2/mieic2009/ei09072/public_html/TP1/Delicious    /Database.db');
   $a = $_GET['linkRef'];
   $b = $_GET['tagToAdd'];

   $checkIdLink = $db->prepare('SELECT idLink FROM Links WHERE nome_L = :link_n;');
   $checkIdLink->bindParam(':link_n', $a, PDO::PARAM_STR);
   $checkIdLink->execute();
   $linkID = $checkIdLink->fetch();

   $insertLink = $db->prepare('INSERT INTO Tags (nome_T, idLink) VALUES (:addTag, :link_id)');
   $insertLink->bindParam(':addTag', $b, PDO::PARAM_STR);
   $insertLink->bindParam(':link_id', $linkID, PDO::PARAM_INT);
   $insertLink->execute();

    echo 'Tag added to the specified link!';
?>

This code should add a Tag to an existing link in a database, however I am getting this error

Fatal error: Call to a member function bindParam() on a non-object in /usr/users2/mieic2009/ei09072/public_html/TP1/Delicious/addTag.php on line 9

I have checked over and over and can't seem to find what's wrong with this code, I googled for this error but unfortunately the answer I found weren't helpful enough. Any help would be appreciated, this is probably a simple rookie mistake.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user697110
  • 589
  • 10
  • 25

2 Answers2

4

I would check that the $db->prepare() function executed successfully. It will return false if it did not. So you could be trying to call bindParam() on a variable that equals false

http://www.php.net/manual/en/pdo.prepare.php

Also you should put the PDO object declaration in try/catch to be sure it was successful as well, as in the first example on this page:

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
  • It could be because of the ; after the query. – GolezTrol Oct 29 '11 at 19:28
  • Thanks, for the help, I already found the problem, I was stupidly acceding an older version of the database, spent over 1 hour looking at this without noticing it. Sorry for wasting your time – user697110 Oct 29 '11 at 19:38
  • The accepted answer is useful, but incorrect for this error message. If this were the problem, the error message would be different. ("Call to a member function **prepare()** on a non-object"). – alttag Jan 04 '17 at 21:54
3

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

Probably the prepare failed so you can't use it.

Savageman
  • 9,257
  • 6
  • 40
  • 50
  • This is the correct answer (not the accepted one). `$db->prepare()` can fail when you have a typo in the SQL statement, or reference a table or attribute that doesn't exist. – alttag Jan 04 '17 at 21:56