Defined variable, still error coming "Undefined Variable"

Hey guys, I made two PHP files, one in which I declared a variable and included it in second file. But When I launch it up all, error comes like this:

Notice: Undefined variable: pdo in C:\xampp\htdocs\includes\access.php on line 61

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\includes\access.php on line [B]61

[/B]

Here are my file codes:

access.php : http://pastebin.com/YdVLMkmT

db.php : http://pastebin.com/PrSayzPD

A little time and help is appreciated from my heart :slight_smile:

That should work I think. My thought is maybe it isn’t finding that file to include. Change the include to a require() and see if you get an error.

Cups gave me a tip when I had a similar problem; give this a go :


include 'db.php';

function databaseContainsAuthor($pdo, $email, $password)


Please make an effort to find the errors yourself it will save you a vast amount of time in the future.

// COMMENT REMOVED

Here is your amended access.php with debugging script.


  try  {    $sql = 'SELECT COUNT(*) FROM author       WHERE email = :email AND password = :password';


echo $sql;  // line added to show the parameter that PDO cannot evaluate.
die;        // stops program execution 

    $s = $pdo->prepare($sql);    $s->bindValue(':email', $email);    $s->bindValue(':password', $password);    $s->execute();  }

I think there is a problem with the email and/or password not being set or incorrect types. Once you know which or both values are incorrect then find why the values are not being set correctly.

Teaching 'em to fish. :slight_smile:

The OP reported the error as “Call to a member function prepare() on a non-object.” The non-object part makes me think that the problem is with the $pdo and not with the $sql.

No, this is not the problem, I tried require() instead of include() but still no success :frowning:

This thing gave me this error:

Warning: Missing argument 3 for databaseContainsAuthor(), called in C:\xampp\htdocs\includes\access.php on line 16 and defined in C:\xampp\htdocs\includes\access.php on line 53

Works correctly and gives out the exact phrase in $sql, so I guess the problem is not with $sql. Problem may be in $pdo.

Warning: Missing argument 3 for databaseContainsAuthor()

Did you include the file outside the function? The error is now looking for 3 variables in the function call and it worked OK before adding the $pdo and I assume it can not find it.

Also if you use the method I posted you only need to include the pdo call once on the page not in every function.