Hi I am having a problem with my index.php whenever I go to http://localhost/blog/admin/index.php I get an error: Fatal error: Call to undefined function Blog\DB\connect() in C:\xampp\htdocs\blog\blog.php on line 6. In admin folder I am requiring my blog.php.
index.php
<?php
require '../blog.php';
?>
Now in blog.php
<?php
require 'functions.php';
require 'db.php';
$conn = Blog\DB\connect($config);
if( !$conn )
{
die('couldnt connect to the database');
}
Now In db.php
<?php namespace Blog\DB;
require 'config.php';
function connect($config)
{
try{
$conn = new \PDO('mysql:host=localhost;dbname=blogs',
$config['DB_USERNAME'],
$config['DB_PASSWRORD']);
$conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(PDOException $e)
{
return false;
}
}
Eventually, you need to have the global namespace like so
$conn = \Blog\DB\connect($config); // mind the \ before Blog
Or the function is simply not defined (connect()
that is).
When you're calling the function you're calling a function from your blog.php not db.php indicated by the error. In order to solve this problem you have to include db.php (Which might already be included by the require) and then call the function normally e.g.
include db.php
$conn = connect($config)