Jump to content

database file error


Iheanetu

Recommended Posts

<?php

// database.php

 

require_once __DIR__ . '/config.php'; // Ensure this path correctly points to config.php

 

/**

 * Establish a new database connection.

 *

 * @return mysqli The MySQLi database connection object.

 * @throws Exception if the connection fails.

 */

function db_connect()

{

    // Use MySQLi to connect to the database

    $connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

 

    // Check if the connection was successful

    if ($connection->connect_error) {

        error_log("Database connection failed: " . $connection->connect_error);

        die("Database connection failed. Please check the error log for details.");

    }

 

    // Set the character set to UTF-8 for proper handling of characters

    if (!$connection->set_charset("utf8mb4")) {

        error_log("Error setting character set utf8mb4: " . $connection->error);

    }

 

    return $connection;

}

 

/**

 * Close an existing database connection.

 *

 * @param mysqli|null $connection The connection object to close.

 * @return void

 */

function db_disconnect($connection)

{

    if ($connection instanceof mysqli) {

        $connection->close();

    }

}

 

// Establish a connection and store it in the variable $db for use later

$db = db_connect();

 

// You can now use $db for your database queries

 

Link to comment
Share on other sites

On 10/29/2024 at 5:07 PM, Iheanetu said:

<?php

// database.php

 

require_once __DIR__ . '/config.php'; // Ensure this path correctly points to config.php

 

/**

 * Establish a new database connection.

 *

 * @return mysqli The MySQLi database connection object.

 * @throws Exception if the connection fails.

 */

function db_connect()

{

    // Use MySQLi to connect to the database

    $connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

 

    // Check if the connection was successful

    if ($connection->connect_error) {

        error_log("Database connection failed: " . $connection->connect_error);

        die("Database connection failed. Please check the error log for details.");

    }

 

    // Set the character set to UTF-8 for proper handling of characters

    if (!$connection->set_charset("utf8mb4")) {

        error_log("Error setting character set utf8mb4: " . $connection->error);

    }

 

    return $connection;

}

 

/**

 * Close an existing database connection.

 *

 * @param mysqli|null $connection The connection object to close.

 * @return void

 */

function db_disconnect($connection)

{

    if ($connection instanceof mysqli) {

        $connection->close();

    }

}

 

// Establish a connection and store it in the variable $db for use later

$db = db_connect();

 

// You can now use $db for your database queries

 

You need to give is some information as to what the actual issue is. Otherwise nobody can or will help you.

Another good tip is to encase your code into the <> tag so it shows like this:

<?php

// database.php



require_once __DIR__ . '/config.php'; // Ensure this path correctly points to config.php



/**

 * Establish a new database connection.

 *

 * @return mysqli The MySQLi database connection object.

 * @throws Exception if the connection fails.

 */

function db_connect()

{

    // Use MySQLi to connect to the database

    $connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);



    // Check if the connection was successful

    if ($connection->connect_error) {

        error_log("Database connection failed: " . $connection->connect_error);

        die("Database connection failed. Please check the error log for details.");

    }



    // Set the character set to UTF-8 for proper handling of characters

    if (!$connection->set_charset("utf8mb4")) {

        error_log("Error setting character set utf8mb4: " . $connection->error);

    }



    return $connection;

}



/**

 * Close an existing database connection.

 *

 * @param mysqli|null $connection The connection object to close.

 * @return void

 */

function db_disconnect($connection)

{

    if ($connection instanceof mysqli) {

        $connection->close();

    }

}



// Establish a connection and store it in the variable $db for use later

$db = db_connect();



// You can now use $db for your database queries

It makes it easier to read.

Also, no need for this:

    if ($connection instanceof mysqli) {

        $connection->close();

    }

PHP automatically closes connections.

Okay, your turn :)

Edited by Moorcam
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.