PHP: DB Connection & Test Code

php logo

Database connection script

 

<?php
/*
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         = "192.168.1.79";
*/
$db_host         = "my.db.host.com";
$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

<?php
// 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>";
    exit();
} else {
    echo "<h1>DB Connection Successful...</h1>";
}
?>

 

 

 

 

 

Tags