0

I have 3 files: index.php, db.php (database), and functions.php

here is an example what is in each file:

database.php:

define ("DB_HOST", "localhost");
define ("DB_USER", "root");
define ("DB_PASS", "1234");
define ("DB_NAME", "test");

try {
    $dsn = "mysql:dbname=".DB_NAME.";host=".DB_HOST;
    $dbh = new PDO($dsn, DB_USER, DB_PASS);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

index.php:

require $_SERVER['DOCUMENT_ROOT']."/config/db.php";
require $_SERVER['DOCUMENT_ROOT']."/config/functions.php";

if(isLoggedIn()) {
    echo "hi";
}

functions.php:

function isLoggedIn() {
    require $_SERVER['DOCUMENT_ROOT']."/config/db.php";

    $stmt = $dbh->prepare("SELECT * FROM users....");
    $stmt->execute();
}

The error i get is:

Notice: Constant DB_HOST already defined in /var/www/config/database.php

what i tried:

I tried to replace require with require_once in all my files but the error that it gives is here:

Fatal error: Uncaught Error: Call to a member function prepare() on null in functions.php

John Jaoill
  • 159
  • 2
  • 9
  • 2
    change all your `require` to `require_once` – LF00 Jun 14 '17 at 03:53
  • Possible duplicate of [Difference between require, include and require\_once?](https://stackoverflow.com/q/2418473/6521116) – LF00 Jun 14 '17 at 03:55
  • 2
    Even better, remove the `require` from `functions.php` and pass `$dbh` to your `isLoggedIn` function as an argument – Phil Jun 14 '17 at 03:55
  • Also, your application will continue to run if PDO cannot connect to your DB. I suggest putting an `exit` in your `catch` block or removing the `try...catch` completely – Phil Jun 14 '17 at 03:56
  • @KrisRoofe sorry please look at my edit – John Jaoill Jun 14 '17 at 03:57
  • @phil yes that solutions seems hacky tho... also, i will put exit thanks – John Jaoill Jun 14 '17 at 03:58
  • @JohnJaoill in what way is [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) *"hacky"*? – Phil Jun 14 '17 at 03:58
  • As @Phil says, injection is a standard method to pass resources, I think what you have is fine (if it was reworked), but it is closer to "hacky" than injection is. – Rasclatt Jun 14 '17 at 04:00
  • I see that require is actually not the problem here. The problem is in this line: `require $_SERVER['DOCUMENT_ROOT']."/config/db.php";` It should be: `require $_SERVER['DOCUMENT_ROOT']."/config/database.php";` You have your file named database.php meanwhile you are trying to import db.php. Just correct that and you should be fine. – pasignature May 07 '19 at 10:15

1 Answers1

2

So, since it didn't make a sense to you my last answer here is a full answer of what you can do to get rid of this problem once for all:

db.php

<?php
$host="localhost"; 
$user="root"; 
$password="1234"; 
$db="test"; 

    try {
        $dbh = new PDO("mysql:host=$host;dbname=$db",$user,$password);
    } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
    }
?>

index.php

require_once $_SERVER['DOCUMENT_ROOT']."/config/db.php";

if(isLoggedIn($dbh)) {
    echo "hi";
}

functions.php

function isLoggedIn($dbh) {
$stmt = $dbh->prepare("SELECT * FROM users....");
$stmt->execute(); 
}
function anotherfunction($dbh) {
$stmt = $dbh->prepare("SELECT * FROM others....");
$stmt->execute(); 
}

The above corrections will save you the pain of looking after the requires, you require the db.php once so that now you have access to its variable and pass as argument whenever needed.

Note: Thanks Phil for fruitful comments and good elaboration.

Oussama Ben Ghorbel
  • 2,132
  • 4
  • 17
  • 34