Jump to content

PDO: including connection.php in another file


Big_Pat
Go to solution Solved by Eiseth,

Recommended Posts

I'm stumped.

 

I'm trying to convert my site to PDO from MySQL and, as part of this, I want to have the connection.php and constants.php files separate from the rest of the content. In my MySQL setup I have a header.php (which REQUIRES connection.php and constants.php), and an index.php which requires header.php. Then there's a functions.php which is called by header.php, and in functions.php I have all my queries and output.

 

I'm trying to adapt this to PDO but I'm unable to connect to the database when I hold the connection info in a separate file. If I move the same connection info into functions.php I can connect fine. I thought of declaring a global in connection.php, but that hasn't resolved, and I also thought of calling the connection as a function but that, too, hasn't worked.

 

In connection.php (with the passwords, etc, changed) I have:

$connection = new PDO('mysql:host=localhost;dbname=mydbname', 'myusername', 'mypassword');

and, in functions.php I have, for example,

$query = $connection->prepare("SELECT COUNT(*) from songlist");

but I get an error: "Fatal error: Call to a member function prepare() on a non-object " each time I try. As I say, if I copy that exact line into functions.php I get no error.

 

I've read a few sites trying to find a solution (one suggested calling a class, which I copied but didn't resolve) and the other suggested a function. What's the secret? I don't want to put the long connection parameter in each time as that defeats the object.

 

 

 

 

Link to comment
Share on other sites

  • Solution

try inserting it into a function

// Put this in your function.php
function connect_db()
{
    try
    {
        $connection = new PDO('mysql:host=localhost;dbname=mydbname', 'myusername', 'mypassword');
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $connection->setAttribute(PDO::ATTR_PERSISTENT, true);
    }
    catch (PDOException $e)
    {
        // Proccess error
        echo 'Cannot connect to database: ' . $e->getMessage();
    }

    return $connection;
}

// Then include the functions.php in your file and call it
$dbh = connect_db();

// Perform query
// put this inside try/catch to catch the error
// You don't need to prepare statements if you're not inserting input from outside source
$stmt = $dbh->query('SELECT COUNT(*) FROM songlist');
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.