SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Location
    India
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy 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 61




    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

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    - Robert

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,364
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    Cups gave me a tip when I had a similar problem; give this a go :
    Code:
    include 'db.php';
    
    function databaseContainsAuthor($pdo, $email, $password)

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,805
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by aakashekhar View Post

    ...
    ...
    ...

    A little time and help is appreciated from my heart
    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.
    PHP Code:
      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.
    Last edited by John_Betong; Feb 7, 2014 at 20:53. Reason: reference to incorrect previous post.

  5. #5
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    Please make an effort to find the errors yourself it will save you a vast amount of time in the future. ... Here is your amended access.php with debugging script.
    Teaching 'em to fish.

    Quote Originally Posted by John_Betong View Post
    echo $sql; // line added to show the parameter that PDO cannot evaluate.
    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.
    "First make it work. Then make it better."

  6. #6
    SitePoint Member
    Join Date
    Apr 2013
    Location
    India
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    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.
    No, this is not the problem, I tried require() instead of include() but still no success

    Quote Originally Posted by Rubble View Post
    Cups gave me a tip when I had a similar problem; give this a go :
    Code:
    include 'db.php';
    
    
    function databaseContainsAuthor($pdo, $email, $password)
    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


    Quote Originally Posted by John_Betong View Post
    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.
    PHP Code:
      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.
    Works correctly and gives out the exact phrase in $sql, so I guess the problem is not with $sql. Problem may be in $pdo.

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,364
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •