PHP & MySQL Connection Script

start your next MySQL database connection with this script


Tuts Library »

<?php
// Using mysqli commands to connect to a MySQL database
// This function requires four (4) parameters: your host path, the admin account, the admin password and the name of the database

#: Connect with Database
// Create variables ans assign their values.
   $db_host = 'localhost';
   $db_user = 'username';
   $db_pass = 'user password';
   $db_name = 'database name';
   
   $dbc = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
   
   if (mysqli_connect_errno()) {
   
      echo 'Cannot connect at this time because: '.mysqli_connect_error();
      echo '<br>Please check back later.';
      exit();
      
   } else {
   
      echo 'You Have Connected Successfully To The Database!';
      
   }
?>