0

I am new to php. I encountered the following errors.

Warning: require_once(/dbtest/db_connect.php): failed to open stream: No such file or directory in C:\xampp\htdocs\dbtest\connect.php on line 2

Fatal error: require_once(): Failed opening required 'dbtest/db_connect.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\dbtest\connect.php on line 2

I am very sure I have db_connect.php file inside my dbtest folder. Please help

I

config.php

<?php
    define ( 'DB_HOST', 'localhost' );
    define ( 'DB_USER', 'root' );
    define ( 'DB_PASSWORD' );
    define ( 'DB_NAME', 'mydb' );
?>

db_connect.php

<?php
/**
 * A class file to connect to database
 */
class DB_CONNECT {

    // constructor
    function __construct() {
        // connecting to database
        $this->connect();
    }

    // destructor
    function __destruct() {
        // closing db connection
        $this->close();
    }

    /**
     * Function to connect with database
     */
    function connect() {
        require_once '/dbtest/config.php';

        $con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

        // Check connection
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        else {
            echo 'Connection Successful';
        }
    }

    /**
     * Function to close db connection
     */
    function close() {
        // closing db connection
        mysql_close();
    }

}
?>

connect.php

<?php
    require_once '/dbtest/db_connect.php';
    $db = new DB_CONNECT();
    $db->connect();
?>

Edited by rowen_1

2
Contributors
5
Replies
21
Views
1 Year
Discussion Span
Last Post by RudyM
0

Is your connect.php file in the htdocs folder, not in dbtest? Sorry, but these things happen sometimes, so I'd like to be sure.

0

Wait, your connect.php file should be in htdocs. The script looks to import db_connect.php in dbtest folder from current location.

0

I think I figured it out. it should be

    require_once '/config.php'; and  require_once '/connect.php';
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.