PDO :: __ construct():MySQL服务器已经消失

时间:2017-03-03 17:06:36

标签: php mysql pdo

这似乎是一个常见问题,我已经阅读了很多关于它尝试使用'ini_set'来更改超时并且仅在5秒后仍然出现此错误的内容。下面是我的代码示例,用于显示类之间的流程。

我最困惑的是它在5秒后(甚至在页面刷新之后)抛出此警告,但它仍然从数据库中检索数据。

    require('../cfg/config.php');

class Process {

    /**
     * @var PDO
     */
    private $conn = null;
    /**
     * @var PDOException
     */
    public $error = null;
    private $host = DB_SERVER;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $db = DB_NAME;

    public function __construct() {
        ini_set('mysql.connect_timeout', 300);
        ini_set('default_socket_timeout', 300);
        $this->connect();
    }

    private function connect() {

        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->db;
        $options = [
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION
        ];
        try {
            $this->conn = new PDO($dsn, $this->user, $this->pass, $options);
        } catch(PDOException $e) {
            $this->error = $e->getMessage();
        }

        return $this->conn;
    }

    public function query($sql, $params = null) {

        preg_match('/SELECT/', $sql) ? $select = true : $select = false;

        $connect = $this->conn->getAttribute(PDO::ATTR_CONNECTION_STATUS);

        echo "CONNECTION STATUS::: " . $connect;

        if($this->error == null) {

            $query = $this->conn->prepare($sql);
            $query->execute($params);

            $query ? (!$select ? $results = true : $results = $query->fetchAll(PDO::FETCH_ASSOC))
                : $results = false;
        } else {
            $results = $this->error;
        }

        return $results;
    }


}

class Arrays {
    public $family = [];
    /**
     * @var \Process
     */
    private $db;

    public function __construct() {

        $this->db = new Process();

    }
    public function setFamily() {
        $a = [];

        $sql = "SELECT familyNick, familyName FROM budget.family ORDER BY familyOrder";
        $query = $this->db->query($sql);

        if($query != false && is_array($query)) {
            foreach($query as $k => $v) {
                $a[$v['familyNick']] = $v['familyName'];
            }
        }

        $this->family = $a;
    }
    /**
     * @return array
     */
    public function getFamily() {

        if(empty($this->family) || !isset($this->family)) {
            $this->setFamily();
        }

        return $this->family;
    }

}

class Options {

    public $family = [];

    /**
     * @var \Arrays
     */
    private $arrays;

    public function __construct() {
        $this->arrays = new Arrays();
    }

    public function setFamily() {

        $this->family = $this->arrays->getFamily();
    }
    public function getFamilyOptions() {

        if(empty($this->family)) {
            $this->setFamily();
        }

        $options = [];

        foreach($this->family as $famNick => $famName) {
            $options[] = "<option class='family' value='$famNick'>" . $famName . "</option>";
        }

        return $options;

    }


}


$test = new Options();

$family = $test->getFamilyOptions();

var_dump($family);

我确定此代码存在各种设计缺陷。我现在只关心PDO警告。

1 个答案:

答案 0 :(得分:1)

我试图完成的只是删除

ATTR_PERSISTENT => true
从选项数组

做了伎俩!