Last active
October 11, 2015 05:28
-
-
Save zmiftah/3810324 to your computer and use it in GitHub Desktop.
PHP: Db (Mysql) Logger Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DbLogger { | |
private $table_name; | |
private $db; | |
private $queries; | |
private $errors; | |
private $result; | |
private $safe; | |
private $connection; | |
public function __construct( $table_name, $safe = false ) { | |
global $con; | |
$this->db = ''; | |
$this->errors = ''; | |
$this->table_name = $table_name; | |
$this->queries = array(); | |
$this->safe = $safe; | |
$this->connection = $con; | |
} | |
public function OpenDB( $server, $username, $password, $database ) { | |
if ( $this->safe ) { | |
$this->db = $this->connection; | |
} else { | |
$this->db = mysql_connect( $server, $username, $password ); | |
mysql_select_db( $database ); | |
} | |
if ( $this->db ) { | |
$this->errors = mysql_error(); | |
} | |
} | |
public function AddEvent( $arrEvent ) { | |
if ( $this->db ) { | |
$sql = $this->buildQuery( $arrEvent ); | |
array_push( $this->queries, $sql ); | |
} | |
} | |
public function GetQueries() { | |
if ( is_array( $this->queries ) ) { | |
$multi_sql = "INSERT INTO $this->table_name ( remote_addr, request_uri, `table`, event, person, status, message, log_date ) VALUES "; | |
$multi_sql .= implode( ',', $this->queries ) . ";"; | |
return $multi_sql; | |
} | |
} | |
public function Execute() { | |
if ( $this->db ) { | |
$multi_sql = $this->GetQueries(); | |
$this->result = mysql_query( $multi_sql ); | |
if ( $this->result ) { | |
$this->errors = mysql_error(); | |
} | |
} | |
} | |
public function ClearQueries() { | |
$this->queries = array(); | |
} | |
public function GetErrors() { | |
return $this->errors; | |
} | |
public function CloseDb() { | |
if ( $this->db ) { | |
mysql_close( $this->db ); | |
} | |
} | |
private function buildQuery( $arrEvent ) { | |
$sql = "( | |
'$arrEvent[remote_addr]', '$arrEvent[request_uri]', '$arrEvent[table]', '$arrEvent[event]', | |
'$arrEvent[person]', '$arrEvent[status]', '$arrEvent[message]', CURRENT_TIMESTAMP | |
)"; | |
return $sql; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment