I have a problem. I am including my database details in my functions.php file. Like this:
db.php:
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "root";
$dbc = new PDO("mysql:host=" . $dbhost . "; dbname=" . $dbname ."", $dbuser, $dbpass);
functions.php:
include_once 'db.php';
function retrieve($count)
{
$query = "SELECT * FROM table WHERE column = ?";
$sth = $dbc->prepare($query);
$sth->bindValue(1, $count);
$sth->execute();
$resultCount = $sth->rowCount();
}
file.php:
include 'functions.php';
...
When I try to call my functions file in file.php, so I can use the function retrieve, my php_error.log file shows the following:
[Date] PHP Notice: Undefined variable: dbc in functions.php on line 6
My goal is to create an instance of PDO and use that in all the other pages so I don't have to create an instance over, and over again. I tried to achieve this by creating the $dbc variable and instancing a new PDO connection to it in db.php. Then, I would include it in functions.php so whenever a function is called, the $sth variable will hold the methods used, using $dbc (the instance of the PDO connection).
Does anyone know why am I getting this error, and possibly, could anyone give me some advice on how to parametrize the creation of an instance of PDO and use it/them throughout other pages?
Thank you for your time and help!
Cheers!