Skip to content

Instantly share code, notes, and snippets.

@macloo
Created January 17, 2024 16:04
Show Gist options
  • Select an option

  • Save macloo/af4872230ed37d4e9bc92c46fb962d3d to your computer and use it in GitHub Desktop.

Select an option

Save macloo/af4872230ed37d4e9bc92c46fb962d3d to your computer and use it in GitHub Desktop.

Revisions

  1. macloo created this gist Jan 17, 2024.
    25 changes: 25 additions & 0 deletions database.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?php
    // allow error reporting
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    ?>
    <?php

    try {
    // connect to database
    $conn = mysqli_connect("localhost", "root", "", "myshoutbox");

    // check connection
    if (!$conn) {
    throw new Exception();
    }
    } catch (Exception $e) {
    // if connection fails, display error message
    echo "Error: Unable to connect to database. ";
    echo " Reason: " . $e->getMessage();
    echo ". mysqli_ error number: " . mysqli_connect_errno();
    exit; // don't execute any other code on the page
    }

    ?>