Skip to content

Instantly share code, notes, and snippets.

@antonga23
Created April 29, 2021 15:50
Show Gist options
  • Save antonga23/62ede8ab29d5ed7743a4bdcefd2ddf5e to your computer and use it in GitHub Desktop.
Save antonga23/62ede8ab29d5ed7743a4bdcefd2ddf5e to your computer and use it in GitHub Desktop.
DB Connection test for php
<?php
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1', // better than localhost if mysqli.default_socket points to a unknown path
'username' => 'db_user', // As of MySQL v5.7, you can't use 'root' without sudo - you must use a different username and password - https://askubuntu.com/questions/763336/cannot-enter-phpmyadmin-as-root-mysql-5-7
'password' => 'password',
'database' => 'dbname',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
/* Connection test: */
echo '<pre>';
print_r($db['default']);
echo '</pre>';
echo 'Connecting to database: ' .$db['default']['database'];
$mysqli_connection = new MySQLi($db['default']['hostname'],
$db['default']['username'],
$db['default']['password'],
$db['default']['database']);
if ($mysqli_connection->connect_error) {
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
echo "Connected.";
}
die( 'file: ' .__FILE__ . ' Line: ' .__LINE__);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment