I connect to MySQL using a PDO and all works fine. To tidy things up I have made an include file that contains the PDO code. This is the full include file:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=******', '******',
'******');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec('SET NAMES "utf8"');
printf('hello');
?>
The print hello was just to check the file was being included, which it is. The problem is I now get this error when loading the page
"Fatal error: Call to a member function query() on a non-object in /home3/danville/public_html/test/parkattractions.php on line 9"
Line 9 is $result = $pdo->query($query);
It was all working fine when the db-connect file was part of the main page, it's only now using it as an include I get the error. What has gone rong and how do I fix it?
EDIT: This is the very top of the page getting the error:
<?php
include 'http://www.themeparkfocus.com/db-connect.php';
try
{
$park_id = $_GET['park_id'];
$query="SELECT * FROM tpf_parks WHERE park_id = $park_id";
$result = $pdo->query($query);
}
catch (PDOException $e)
{
$output = 'Unable to connect to the database server.';
//include 'output.html.php';//
}