Object Oriented Style

include_once('config.php');

//----- CONNECT TO DATABASE -----
$maindb = new mysqli($main_db_address, $main_db_username, $main_db_password, $main_db_name);
if ($maindb->connect_error)
{
  die("Error: Unable to connect to MySQL: " . $maindb->connect_error);
}

//Turn on mysqli throwing exceptions by itself (no if() echo $maindb->error required)
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
config.php
<?php
  //Main Database:
  $main_db_address = 'locahost';			//Typically "localhost" or IP address / URL if on another server
  $main_db_name = 'MY_DATABASE_NAME';
  $main_db_username = 'USER_NAME';
  $main_db_password = 'PASSWORD';
?>

Procedural Style

include_once('config.php');

//----- CONNECT TO DATABASE -----
$dblink = mysqli_connect($main_db_address, $main_db_username, $main_db_password, $main_db_name);
if (!$dblink)
{
  echo "Error: Unable to connect to MySQL." . PHP_EOL;
  echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
  echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
  die();
}

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *