user4592091
user4592091

Reputation:

Database Connection not Working as Include File

I want to move the my database connection info to an include file - normally, the database connects fine when I have it written out in the entire php header like this:

   <?php

        define('DB_LOCATION', 'x');
        define('DB_USERNAME', 'x');
        define('DB_PASS', 'x');
        define('DB_NAME', 'x');

        $dbc = mysqli_connect(DB_LOCATION, DB_USERNAME, DB_PASS, DB_NAME)
            or die('Error connecting to database');

        $error_message= "";

        $user_name = $_POST['user'];
        $user_password= $_POST['pass'];

        if (isset($_POST['submit'])) {

            ......
        }

Here, I moved this info to it's own file as an include file. It's saved as a (.php) file.

  <?php

        define('DB_LOCATION', 'x');
        define('DB_USERNAME', 'x');
        define('DB_PASS', 'x');
        define('DB_NAME', 'x');

        $dbc = mysqli_connect(DB_LOCATION, DB_USERNAME, DB_PASS, DB_NAME)
            or die('Error connecting to database');


    ?>

Now, I replaced database connection with the include file mentioned above. Both the script and the include file are in the same directory/folder. However when I go to my page with the require_once include in the header, the page is blank. I'm not getting a database error which means it is connecting, but I am not seeing my content either.

Could it be that that the location is not properly specified? Both are under file /LESSON. I also tried using @require_once and it did not help. Any input would be greatly appreciated.

<?php

require_once("/LESSON5/Lesson_5_DB_Connection.php");

?>

<?php 

    session_start();

    $error_message= "";

    $user_name = "";

    ......
?>

Upvotes: 0

Views: 1358

Answers (1)

Aman Rawat
Aman Rawat

Reputation: 2625

if your both files are in same directory then there is no need to give the name of directory just give the name of the file.

In your case use

require_once("Lesson_5_DB_Connection.php");

and if you use @ in front of your function call to suppress all error messages. Thats why you are getting blank page

Upvotes: 1

Related Questions