简体   繁体   中英

PDO::__construct(): MySQL server has gone away

this seems to be a common issue, and I've read a lot about it tried to use 'ini_set' to change time outs and still getting this error after only 5 seconds. below is a sample of my code to show the flow between classes.

the thing that i'm most confused about is that it throws this warning after 5 seconds (even after a page refresh), but it still retrieves the data from the database.

    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);

i'm certain there are various design flaws to this code. i'm just concerned about the PDO warning at this point.

for what i was trying to accomplish simply removing

ATTR_PERSISTENT => true

from the option array did the trick!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM