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();
?>
Member Avatar for RudyM

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.

I tripled checked. it's in dbtest, not in htdocs

Member Avatar for RudyM

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

I think I figured it out. it should be

    require_once '/config.php'; and  require_once '/connect.php';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.