php - How to insert a row in the table if there is an connection error? -


i want enter record in table "logs" if there connection error,so how do that?

logs table structure is

create table logs( id int(20) auto_increment primary key, class_name varchar(20), function_name varchar(20),  error varchar(200), error_date datetime)); 

this database.php

<?php class database{     public $server = "localhost";     public $user = "root";     public $password = "";     public $table_name = "";     public $database_name = "todo_application";     public $class_name = "database";     public $function_name = "";     public $database_connection = "";      //constructor     public function __construct(){         //establishes database connection         $this->database_connection = new mysqli($this->server,                                                 $this->user,                                                 $this->password,                                                 $this->database_name);         if($this->database_connection->connect_error)             die("connection failed".$this->database_connection->connect_error);          }      //destructor     public function __destruct(){         //terminates database connection         $this->database_connection->close();     }      //function run database query.     //input : database query.     //output : returns array of result.     public function run_query($sql_query){         $this->function_name = "run_query";         try{             if(!$this->database_connection){                 $output = $this->database_connection->query($sql_query);                 if($output){                     $result["status"] = 1;                     $result["array"] = $output;                 }                 else{                     $result["status"] = 0;                     $result["message"] = "query error";                 }             }             else{                 throw new exception ("connection error");             }         }         catch(exception $error){             $result["status"] = 0;             $result["message"] = $error->getmessage();             $this->error_table($this->class_name, $this->function_name, "connection error", date('y-m-d h:i:s'));            }         return $result;     }     //function insert error in table     //input : class name, function name , error & date.     public function error_table($class_name, $function_name, $error_message, $date){             $sql_query = "insert `logs`( `class_name`, `function_name`, `error`, `error_date`) values ('$class_name', '$function_name', '$error_message', '$date')";             $this->database_connection->query($sql_query) ;     }  ?> 

calling.php

<?php  include "database.php";  $connection = new database();  $sql_query = "select * some_tablename";  $output = $connection->run_query($sql_query);  ?> 

can point out wrong, , guide me find solution? in advance.

if intend log error in same mysql instance couldn't connect to, answer is: can't this.

when application can't connect database, won't able save error messages in it.

what try connect again few times (hoping connection errors go away), there no guarantee can ever establish connection. it's not safe rely on that.

alternatives use database instance logging problems, logging errors text file, sending email, or using php's error_log function log error.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -