mysql - Autoreconnect class for database in php -
i developing class database access , query. if database accessible working correctly, problem comes when somehow database restarted.
below code library , users code. able reconnect database, unable select database. have tried 2,5 retry attempts select_from_db function. throwing error unable select database test mysql server has gone away. using windows machine , in sleep time executing below command restarting mysql.
net stop wampmysqld net start wampmysqld
please let me know suggestion , answers, can improve on it.
<?php class database { private $host; private $user; private $pswd; private $name; private $db_handle; private $error_msg; function __construct($db_host, $db_user, $db_pswd, $db_name=null){ $this->host = $db_host; $this->user = $db_user; $this->pswd = $db_pswd; $this->name = $db_name; $this->db_handle = null; $this->error_msg = null; } function connect(){ if(!is_null($this->db_handle)){ return true; } echo "trying connection..\n"; $this->db_handle = mysql_connect($this->host, $this->user, $this->pswd); if(!$this->db_handle){ $this->db_handle = null; $this->error_msg = "unable connect database : ".mysql_error(); return false; } else{ echo "\nconnected database successfully!\n"; } echo $this->name. "\n"; if(!is_null($this->name)){ $result = mysql_select_db($this->name, $this->db_handle); if(!$result){ $this->db_handle = null; $this->error_msg = 'unable select database '.$this->name.' '.mysql_error(); return false; } else{ echo "\nselected database successfully!\n"; } } return true; } function select_from_db($query, $retry=true){ if(!$this->is_connected()){ $result = $this->connect(); echo " 45 : $result\n"; if(!$result) return $result; } echo " executing query \n"; $result = mysql_query($query, $this->db_handle); if(!$result){ if(stristr(mysql_error(), 'mysql server has gone away') === false){ $this->error_msg = "unable execute query ".mysql_error(); } else{ if($retry){ $this->db_handle = null; $result = $this->connect_wait(30,5); return $this->select_from_db($query, false); } } } echo mysql_error(); echo " returning result $result query \n"; return $result; } function insert_to_db($query){ $result = $this->select_from_db($query); if(!$result) return $result; if(stripos($query, 'insert into') !== false){ return mysql_insert_id($this->db_handle); } return $result; } function is_connected(){ if(is_null($this->db_handle)) return false; return true; } function connect_wait($seconds_to_wait=30, $no_iterations=-1){ $result = $this->connect(); for($counter = 0; ($counter < $no_iterations) && !$result; $counter++){ echo "** $counter \n"; echo "\n$result waiting\n"; sleep($seconds_to_wait); $result = $this->connect(); echo "\n$result\n"; } return $result; } function disconnect() { if(!is_null($this->db_handle)){ mysql_close($this->db_handle); $this->db_handle = null; } } function get_error() { return $this->error_msg; } function __destruct() { $this->disconnect(); } } // connection database machine $db_host = 'localhost'; $db_user = 'root1'; #$db_pswd = 'rtpswd$567'; $db_pswd = ''; $db_name = 'test'; $db_table = 'user_login_info'; $db_obj = new database($db_host, $db_user, $db_pswd, $db_name); if($db_obj->connect()){ echo "connected"; sleep(20); echo "querying"; $query = "select * $db_table"; $result = $db_obj->select_from_db($query); if(!$result){ echo mysql_error(); die($db_obj->get_error()); } while($row = mysql_fetch_assoc($result)){ echo "{$row['user_login_name']} {$row['user_password']}\n"; } $db_obj->disconnect(); } ?>
pdo library, don't have write own ! can add class https://gist.github.com/extraordinaire/4135119 , use automatically reconnect when database away.
Comments
Post a Comment