Skip to content

Instantly share code, notes, and snippets.

@asharirfan
Last active May 2, 2019 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asharirfan/c41b7487bc11fb8c2ec097ae1b2e8051 to your computer and use it in GitHub Desktop.
Save asharirfan/c41b7487bc11fb8c2ec097ae1b2e8051 to your computer and use it in GitHub Desktop.
💾 DB Connectivity Tests
<?php
/**
* DB Connectivity Tests
*
* @package dbtest
*/
// Define database constants.
define( 'DBTEST_HOST', 'localhost' ); // REPLACE THE CONSTANT VALUE WITH YOUR HOST.
define( 'DBTEST_USER', 'root' ); // REPLACE THE CONSTANT VALUE WITH YOUR USERNAME.
define( 'DBTEST_PASSWORD', 'root' ); // REPLACE THE CONSTANT VALUE WITH YOUR PASSWORD.
define( 'DBTEST_DATABASE', 'local' ); // REPLACE THE CONSTANT VALUE WITH YOUR DATABASE NAME.
$db_connection = mysqli_connect( DBTEST_HOST, DBTEST_USER, DBTEST_PASSWORD, DBTEST_DATABASE ); // phpcs:ignore
if ( ! $db_connection ) {
echo '<h2>Error: ' . mysqli_connect_error() . '</h2>' . PHP_EOL; // phpcs:ignore
echo '<h3>Error number: ' . mysqli_connect_errno() . '</h3>' . PHP_EOL; // phpcs:ignore
exit;
} else {
echo '<h3>1. Successfully connected to MySQL database server.</h3>' . PHP_EOL;
$select_db = mysqli_select_db( $db_connection, DBTEST_DATABASE ); // phpcs:ignore
if ( ! $select_db ) {
echo '<h3>Unknown database "' . DBTEST_DATABASE . '"</h3>' . PHP_EOL; // phpcs:ignore
exit;
} else {
echo '<h3>2. WordPress MySQL database selected.</h3>' . PHP_EOL;
}
mysqli_close( $db_connection ); // phpcs:ignore
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment