php中数据库的自动连接类

时间:2016-11-13 14:51:51

标签: php mysql

我正在开发一个用于数据库访问和查询的类。 如果数据库是可访问的,它可以正常工作,但是当某个数据库重新启动时会出现问题。

以下是库及其用户代码的代码。 它能够与数据库重新连接,但无法选择数据库。我尝试过select_from_db函数的2,5次重试尝试。但它的抛出错误无法选择数据库测试MySQL服务器已经消失。我正在使用Windows机器,在睡眠时间我执行下面的命令重启mysql。

net stop wampmysqld
net start wampmysqld

请告诉我您的建议和答案,以便我可以改进。

<?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 to connect to database : ".mysql_error();
                return False;
            }
            else{
                echo "\nconnected to 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 to 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 to 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 to 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 * from $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();
}
?>

1 个答案:

答案 0 :(得分:0)

PDO是一个很好的图书馆,你不必自己编写! 您可以添加类https://gist.github.com/extraordinaire/4135119,并在数据库不在时使用它自动重新连接。