Skip to content

Instantly share code, notes, and snippets.

@tinshade
Created November 18, 2020 08:25
Show Gist options
  • Save tinshade/f5b94310e8742a665f8f23d13e571c63 to your computer and use it in GitHub Desktop.
Save tinshade/f5b94310e8742a665f8f23d13e571c63 to your computer and use it in GitHub Desktop.
Simple PHP database configuration script that automatically switches between local and production databases based on the server it is being run on.
<?php
$whitelist = array('127.0.0.1','::1');
if(!in_array($_SERVER['REMOTE_ADDR'], $whitelist)){
//SERVER
$server = 'localhost';
$username = 'server_username';
$password = 'server_password';
$dbname = 'username_dbname';
$conn = mysqli_connect($server, $username, $password, $dbname);
if (!$conn){
die("Connection Failed! ".mysqli_connect_error());
}
}else{
//LOCAL
$server = 'localhost';
$username = 'root';
$password = '';
$dbname = 'dbname';
$conn = mysqli_connect($server, $username, $password, $dbname);
if (!$conn){
die("Connection Failed! ".mysqli_connect_error());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment