Skip to content

Instantly share code, notes, and snippets.

@mostafasoufi
Last active September 5, 2017 07:06
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 mostafasoufi/84cf2d6ba7f461b00136d7b2ac41cdd6 to your computer and use it in GitHub Desktop.
Save mostafasoufi/84cf2d6ba7f461b00136d7b2ac41cdd6 to your computer and use it in GitHub Desktop.
PHP & MySQL checker
<?php
/**
* Class Service_Checker
* @author Mostafa Soufi
*/
class Service_Checker {
/**
* @var array
*/
public $error_message = array();
/**
* Service_Checker constructor.
*
* @param array $config
*
* @internal param array $db_config
*/
public function __construct( $config = array() ) {
// Check php
$this->php_checker();
// Check MySQL
$this->mysql_checker( $config );
}
/**
* PHP checker
*/
public function php_checker() {
if ( ! function_exists( 'phpinfo' ) ) {
$this->error_message[] = 'PHP is not running.';
}
}
/**
* MySQL checker
*
* @param $config
*
* @internal param $host
* @internal param $username
* @internal param $password
*/
public function mysql_checker( $config ) {
mysqli_report( MYSQLI_REPORT_STRICT );
if ( ! function_exists( 'mysqli_connect' ) ) {
$this->error_message[] = 'MySQLi is not running.';
return;
}
try {
$link = mysqli_connect( $config['host'], $config['username'], $config['password'], $config['db_name'], $config['port'] );
if ( mysqli_connect_errno() ) {
$this->error_message[] = "Mysql Error: " . mysqli_connect_error();
return;
}
if ( ! mysqli_query( $link, "SHOW VARIABLES" ) === true ) {
$this->error_message[] = 'MySQLi is not running.';
return;
}
// Create table
$check_table = mysqli_query( $link, "SHOW TABLES LIKE '" . $config['table_name'] . "'" );
if ( ! $check_table->num_rows ) {
// Create table
mysqli_query( $link, "CREATE TABLE `" . $config['table_name'] . "` ( `ID` INT NOT NULL AUTO_INCREMENT , `meta_key` VARCHAR(255) NOT NULL , `value` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;" );
}
// Check insert
$check_insert = mysqli_query( $link, "INSERT INTO `" . $config['table_name'] . "` (`ID`, `meta_key`, `value`) VALUES (NULL, 'name', 'michael');" );
if ( ! $check_insert ) {
$this->error_message[] = "Can't insert to database.";
return;
}
// Check delete
$check_delete = mysqli_query( $link, "DELETE FROM `" . $config['table_name'] . "`" );
if ( ! $check_delete ) {
$this->error_message[] = "Can't delete data from database.";
return;
}
} catch ( Exception $e ) {
$this->error_message[] = 'Mysql Error: ' . $e->getMessage();
}
}
/**
* Do check
*/
public function check() {
if ( $this->error_message ) {
echo $this->json_response( $this->error_message, 300 );
} else {
echo $this->json_response( 'Everything is good!' );
}
}
/**
* @param null $message
* @param int $code
*
* @return string
*/
private function json_response( $message = null, $code = 200 ) {
// clear the old headers
header_remove();
// set the actual code
http_response_code( $code );
// set the header to make sure cache is forced
header( "Cache-Control: no-transform,public,max-age=300,s-maxage=900" );
// treat this as json
header( 'Content-Type: application/json' );
// return the encoded json
return json_encode( array(
'status' => $code < 300, // success or not?
'messages' => $message
) );
}
}
$db_config = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db_name' => 'checker',
'table_name' => 'checker',
'port' => 3306,
);
$service_checker = new Service_Checker( $db_config );
$service_checker->check();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment