1

I'm an inexperienced php programmer and only found out about PDO a few days ago. I'm now trying to port my website code over to using PDO, but I am getting an error when I try to use the PDO object that I create.

The error I'm getting is:

Fatal error: Call to a member function prepare() on a non-object in ... file2.php ...

The code looks like this:

index.php

class myClass
{
        ... variables ...

        ... functions ...

        public function myFunction() // gets called on page load, outputs content to page
        {
            ... stuff ...

            require('file1.php');

            ... stuff ...
        }
}

file1.php

require_once('mysql_connect.php'); // create pdo object if not created

... stuff ...

require_once('file2.php');

// I can use the PDO object in here to make queries

$output = function2(); // function2 is in file2.php

... stuff ...

file2.php

require_once('mysql_connect.php'); // create pdo object if not created

function function2()
{
    ... stuff ...

    // PDO error occurs here
    $stmt = $db->prepare(...);
    makeQuery($stmt, array(...));

    return $something;
}

mysql_connect.php

try 
{
    $db = new PDO("mysql:$dbhost=localhost;dbname=$dbname;charset=utf8", $dbuser, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (PDOException $e) 
{
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

function makeQuery($stmt, $array = array())
{
    try 
    {
        $stmt->execute($array);
    }
    catch (PDOException $e) 
    {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}
Nate
  • 26,164
  • 34
  • 130
  • 214
  • 1
    We need to see code, but my bet is on the PDO object being out of scope in the function you're using it in, if it was defined inside `method myfunction()` – Michael Berkowski Jul 05 '12 at 15:11
  • 1
    You should post the error, as well as some relavent code with the connection details scrubbed out. – Kurt Funai Jul 05 '12 at 15:11
  • 1
    Can you show us the code, rather than describe it? – Matt Gibson Jul 05 '12 at 15:11
  • possible duplicate of [pdo - Call to a member function prepare() on a non-object](http://stackoverflow.com/questions/5346186/pdo-call-to-a-member-function-prepare-on-a-non-object) – DaveRandom Jul 05 '12 at 15:21
  • @ghbarratt - I updated my question. Also, could you change the file path in your comment to "file2.php"? (both because the question is different, and also because I didn't mean to post that file path. Thanks!) – Nate Jul 05 '12 at 15:43
  • @DaveRandom - I updated my question. Also, could you change the file path in your comment to "file2.php"? (both because the question is different, and also because I didn't mean to post that file path. Thanks!) – Nate Jul 05 '12 at 15:43
  • 1
    Once a comment is 5 minutes old it cannot be edited, but I will delete my comment if you like. So you are declaring the PDO instantiated object as a global $db. Could there be something else attempting to redeclare the global $db? You originally hinted that you were converting to PDO. Do you have a `mysql_connect` somewhere too? – ghbarratt Jul 05 '12 at 15:49
  • @ghbarratt - Thank you, I would appreciate that. the mysql_connect file is the only place a variable named $db is declared or defined. – Nate Jul 05 '12 at 15:51

3 Answers3

1

If I understand your logic right, you're trying to use the PDO object inside myFunction2 - are you passing the PDO object in as a parameter, or declaring it as a global variable? Because if you're not, it's going to be out of scope, and you won't be able to use it.

andrewsi
  • 10,807
  • 132
  • 35
  • 51
  • I just tried adding `global $db;` inside the function where the PDO object is being used and it didn't change anything. – Nate Jul 05 '12 at 16:02
  • Could you try moving the `include('mysql_connect.php')` call inside the function as well, please? – andrewsi Jul 05 '12 at 16:04
  • Well, I could, but I'm not sure it's a good idea since I have several hundred functions that need to make queries. So it seems like it would be a waste to include a file and create a new $db object inside every single function. – Nate Jul 05 '12 at 16:07
  • It's just to make sure that it's not an issue with scope, rather than an actual fix. – andrewsi Jul 05 '12 at 16:12
  • Oh, OK. I added the PDO object creation code inside the function and the error goes away. So, it does appear to be an issue with scope. Since that's the case, however, why didn't declaring `$db` as a global inside the function work? What's the best solution? – Nate Jul 05 '12 at 16:20
  • That's very interesting! My suspicion is that there's something messing with the $db variable before it gets to $file2. Setting it as a global didn't work, because you're just using the same instance of the variable; but including the file again did, because that re-initialised it. You're not closing $db anywhere, are you? – andrewsi Jul 05 '12 at 16:24
  • No, although I've read that one should close the connection after using it, I never have. – Nate Jul 05 '12 at 16:41
  • All I can suggest, in that case, is that on the line after `global $db;` in your function, you add a `var_dump($db);` to see what's in there before it gets re-initialised. – andrewsi Jul 05 '12 at 16:43
  • `var_dump($db)` after `global($db)` ouputs `NULL`. – Nate Jul 05 '12 at 16:48
  • Then there's something interfering with `$db` elsewhere in the code. Try adding that same `var_dump` line in a few other places, and see if you can figure out where it's going from set to `NULL` – andrewsi Jul 05 '12 at 16:50
0

You don't need to include mysql_coonect again. Include it just once.

 index.php
-class myClass defined
--method myFunction defined (it get's called on pageload & returns the page output)
---include file1.php
----require_once('mysql_connect.php') (creates pdo object)
----*I can use the pdo object here successfully*
----require_once('file2.php')
-----function myFunction2 defined
0

Are DSN, username and password correct? If so, you do something like that:

$pdo = new PDO("dsn");
/* Some code... at the moment something changes $pdo value */
$pdo->prepare("QUERY");
pamil
  • 980
  • 2
  • 10
  • 20
  • PDO throws an exception when it fails to connect - the fact that we're not seeing an exception error message tells me the OP either connected successfully of didn't instantiate PDO in the first place. – DaveRandom Jul 05 '12 at 15:15
  • Yeah, I'm not familiar with PDO too much. But definitely something is going on with PDO instance. – pamil Jul 05 '12 at 15:23