2

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;
     }
   } 
8
  • Where's your index.php code? Commented Oct 30, 2015 at 13:08
  • blog.php is the index.php , sorry I am using stack over flow for the first time. Commented Oct 30, 2015 at 13:10
  • If I remember correctly, you need to name blog.php as index.php for there to be an index to go to? Commented Oct 30, 2015 at 13:12
  • Yes I edited it but I really do not understand why does it say Call to undefined function connect() only in that admin directory. Commented Oct 30, 2015 at 13:18
  • You're calling the function wrong, get rid of the whole Blog\DB\ before connect($config) and include db.php. Commented Oct 30, 2015 at 13:20

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

No I tried that I didnt work and sorry blog.php is the index.php.
0

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)

2 Comments

I did the same thing but it doesnt work dont know why ?
No nothing @General Charismo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.