Big_Pat Posted May 20, 2013 Share Posted May 20, 2013 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. Quote Link to comment Share on other sites More sharing options...
Solution Eiseth Posted May 20, 2013 Solution Share Posted May 20, 2013 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'); Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 20, 2013 Share Posted May 20, 2013 Just be careful what order you are calling your requires. it cascades the code from each as it's called so make sure that all declarations are required before any calls Quote Link to comment Share on other sites More sharing options...
Big_Pat Posted May 20, 2013 Author Share Posted May 20, 2013 Thank you Eiseth! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.