PHP: DB Connection & Test Code

php logo

Database connection script


Assign variables for database hostname/ipaddress,  user, pwd, dbname
$myConnection is instantiated to hold the db connection
mysqli_connect()  function takes variables and creates the connection
host can also be an ip address like:
  $db_host         = "";
$db_host         = "";
$db_username     = "username";
$db_pass         = "P@ssworD";
$db_name         = "database_name";
$myConnection     = mysqli_connect($db_host, $db_username, $db_pass, $db_name);


Database Connection Test Script

// Connect to the DB Connection script
require 'db_conx.php';

// Evaluate the connection
if (mysqli_connect_errno()) {
    echo "<h2>Failure:</h2><em>" . mysqli_connect_error() . "</em>";
} else {
    echo "<h1>DB Connection Successful...</h1>";