So I have this pdo.php file that contains my Database connection. This is the function:
function db() {
static $dbh;
if(!isset($dbh)) {
$dsn = sprintf('mysql:host=%s;dbname=%s', SHOP_DB_HOST, SHOP_DB_NAME);
try {
$dbh = new PDO($dsn, SHOP_DB_USER, SHOP_DB_PASSWORD);
} catch(PDOException $e) {
header('Status: 500 Internal Server Error');
echo '<b>Error:</b> Database connection failed.';
exit;
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec('SET NAMES utf8');
}
return $dbh;
}
And this is another file in which I need to use db():(It's detecting an ajax call and based on that, supposed to update information)
if(isset($_POST['updateTree'])) {
$page_id = $_POST['page_id'];
$parent_id = $_POST['parent_id'];
$customer_id = $_SESSION['customer_id'];
$stojkovQ = db()->prepare("
UPDATE
cms_pages
SET
topicId = :parent_id
WHERE
page_id = :page_id
AND
customer_id = :customer_id
");
$stojkovQ->execute(array(
'customer_id' => $customer_id,
'page_id' => $page_id,
'topicId' => $parent_id
));
echo 'updated';
}
So after after getting an "Fatal Error: Call to undefined function db()" even tho it's included in my header.php file(containing all the includes and config information for my site). If I try to include it again just above my query it yields another error(Cannot redeclare db())
Any lead towards the resolving of this problem will be of great help.