成员数。数组输出数?

时间:2016-04-06 19:19:39

标签: php pdo

我目前卡住了..

当尝试获取成员计数时,它返回一个数组,当它是一个数字时输出并不容易..这里是代码

Member.php

public function __construct() { $this->_pdo = DB::connect(); }

public function count() {
    $this->_query = $this->_pdo->query("SELECT COUNT(*) FROM members");
    if(!$this->_query->error()) {
        print_r($this->_query);
    }
    return false;
}

db.php中

    private function __construct() {
    try {
        $this->pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/dbname'), Config::get('mysql/username'), Config::get('mysql/password'));
    } catch(PDOException $e) {
        die($e->getMessage());
    }
}

public static function connect() {
    if(!isset(self::$_connect)) {
        self::$_connect = new DB();
    }
    return self::$_connect;
}

public function query($sql, $params = array()) {
    $this->_error = false;
    if($this->_query = $this->pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }
    return $this;
}

如何以正确的方式输出?

1 个答案:

答案 0 :(得分:1)

您需要获取数据。

$this->_query->fetchColumn();