Skip to content

Instantly share code, notes, and snippets.

@macloo
Created January 17, 2024 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macloo/af4872230ed37d4e9bc92c46fb962d3d to your computer and use it in GitHub Desktop.
Save macloo/af4872230ed37d4e9bc92c46fb962d3d to your computer and use it in GitHub Desktop.
Database connection script for PHP 8.x and MySQL
<?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
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment