0

With school we are learning about Database connections. We are working with W10 and XAMPP, but the problem we come along every time is this throw error:

include(): Failed opening 'db_connection.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\practice\index.php on line 2

We used multiple sources, namely a video from Dani Krossing regarding Database connections with PDO (https://www.youtube.com/watch?v=BaEm2Qv14oU) and a source from the PHP website itself, which is the same that Dani used in his video. The code is the following:

private function connect() {
    try {
        $username = "root";
        $password = "";
        $dbh = new PDO('mysql:host=localhost;dbname=ooplogin', $username, $password);
        return $dbh;    

    }
    catch (PDOException $e){
        print "Error! " . $e->getMessage() . "<br/>";
        die();
    }
}

My question is where the fault is, because the most of us are getting the same error like me.

I tried to do some research, and found this, but I'm not really sure if that'll fix the problem for us.

Please leave some alternative suggestions or solutions, or ask for more info if you aren't coming out with all these sources.

Thanks in advance!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
JV__
  • 1
  • 1
  • 1
    So where is the script `db_connection.php` actually located in your file structure – RiggsFolly Apr 12 '22 at 08:21
  • The error is in `index.php` when it tries to include the file `db_connections.php` the error is not in `db_connection.php` well not this error anyway – RiggsFolly Apr 12 '22 at 08:23
  • Did the tutorial also suggest changing the `include_path` in your `php.ini`? – RiggsFolly Apr 12 '22 at 08:24
  • `db_connection.php` is based in `C:\xampp\htdocs\practice`, as well as for my `index.php`. The tutorial didn't tell anything about changing include_path within the `php.ini` file. It was a full focused tutorial on how to make a login/signup form and connect it onto a database within PHPMyAdmin using PDO. Which line should I seach for to change the `include_path` within `php.ini`? That would be useful to know as well in case I need to change it. – JV__ Apr 13 '22 at 09:39
  • Well the line in your `php.ini` that says `include_path` check there is a `.` in the path like `include_path = ".;c:\php\includes"` – RiggsFolly Apr 13 '22 at 09:47
  • Checked in `php.ini`, and this is what I can find about `include_path`: `include_path=C:\xampp\php\PEAR`, so the fault shouldn't be in here either. – JV__ Apr 13 '22 at 10:00
  • So try `include_path = ".;C:\xampp\php\PEAR";` – RiggsFolly Apr 13 '22 at 10:02
  • Tried that, but doesn't seem to be working... It throws `include(): Failed opening 'db_connection.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\practice\index.php on line 2` once again. – JV__ Apr 13 '22 at 10:09
  • Ok, so I am obviously missing something. I am not about to listen to that tutorial so please show us the code where the include is. It may also be useful to know the directroy structure – RiggsFolly Apr 13 '22 at 10:15

1 Answers1

0

Your include path only contains a single directory:

include_path='C:\xampp\php\PEAR'

This is wrong; you should have the current directory in it, too:

include_path='.;C:\xampp\php\PEAR'

Modify this either in your php.ini and then restart Apache, or use ini_set to change it from your index.php:

ini_set('include_path', '.;C:\xampp\php\PEAR');
cweiske
  • 30,033
  • 14
  • 133
  • 194