使用PDO修复PHP类中的max_user_connections

时间:2016-10-27 16:37:21

标签: php mysql pdo

我一直在调整旧的抽象层来使用PDO,但是在循环遍历大集时遇到user x has more than 'max_user_connections' active connections SQLSTATE[HY000] [1203]错误。我一直在阅读http://php.net/manual/en/pdo.connections.php,但我在循环中取消设置$ dbh的所有尝试都会导致连接结束时出错。

基类看起来像

class DB {  
    public $pdo;
    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $dbname = DB_NAME;

    public function __construct()
    {
        $this->connect();
    }

    private function connect()
    {
         $options = array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        try {
            $this->pdo = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=utf8;", $this->user, $this->pass, $options);
        } catch(PDOException $e) {
            echo $e->getMessage();
        }
    }

    public function __sleep()
    {
        return array('dsn', 'username', 'password');
    }

    public function __wakeup()
    {
        $this->connect();
    }

    public function __destruct()
    {
        $this->connection = null;
        $this->pdo = null;
        unset($this->pdo);
    }

    // CRUD methods follow including

    function retrieve($where, $groupBy='', $order_by='') {

        $query = "SELECT * FROM `$this->table` $where $groupBy $order_by"; 

        $q = $this->pdo->prepare($query);
        $q->execute();

        $result = $q->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE,get_class($this));
        // was $result = $q->fetchAll(PDO::FETCH_CLASS,get_class($this));
        $this->query_log($query);
        $q = null;

        if ($result == 'NULL') {
            return false;
        } else {
            return $result;
        }
    } // retrieve()

有错误的例子是

if (in_array($_GET['type'], $types)) {
    $type = $_GET['type'];
    $rsObj = new ReservedSlug;
    if ($type == 'artist') {
        $obj = new CalendarArtist;
        $slugfield = 'urlSlug';
        $namefield = 'name';
    } else if ($type == 'event') {
        $obj = new CalendarEvent;
        $slugfield = 'urlSlug';
        $namefield = 'name';
    } else if ($type == 'location') {
        $obj = new Location;
        $slugfield = 'UrlSlug';
        $namefield = 'LocationName1';
    }
    $needslug = $obj->retrieve("TRIM(`$namefield`) != '' AND (`$slugfield` = '' OR `$slugfield` IS NULL) LIMIT 0,400");

            if ($needslug) {
                foreach ($needslug as $ns) {
                    $testslug = slugify($ns->$namefield);
                    list($reserved) = $rsObj->retrieve("`slug` = '$testslug' AND `type` = '$type'");
                    if (!$reserved) {
                        list($test) = $obj->retrieve("`$slugfield` = '$testslug'");
                        if ($test) {
                            for ($i = 2; $i < 26; $i++) {
                                list($test) = $obj->retrieve("`$slugfield` = '$testslug-$i'");
                                if (!$test) {
                                    $slug = $testslug . '-' . $i;
                                }
                            }
                        } else { // not found in table
                            $slug = $testslug;
                        }
                    } else { // was reserved
                        $slug = false;  
                    }
                    echo $ns->$namefield . " gets $slug<p>";
                } // foreach needslug       
            } // if needslug
        } // type found in array

因此,我需要了解在活动连接可用时如何不创建新连接以及如何正确__destruct()这些子对象。我哪里错了?

0 个答案:

没有答案