简体   繁体   English

对于不同的查询返回相同的计数值

[英]Same Count Value is Returned for Different Queries

I am writing a simple class and there is a public query function. 我正在编写一个简单的类,并且有一个公共查询功能。 Along with results and some other data, the function returns row count of returned results too. 除了结果和其他数据外,该函数还返回返回结果的行数。 Everything works fine but the count returned is same for all queries. 一切正常,但返回的计数对于所有查询都是相同的。

Example: 例:

    $db = DB::dbInstance();
    $count1 = $db->query("SELECT * FROM users")->count(); // i.e. 10 records
    $count2= $db->query("SELECT * FROM articles")->count(); // still 10 but they should not

As you can see in above queries that both are different and there are different record counts too, still the returned count is same for different queries. 如您在上面的查询中看到的,两者都是不同的,并且记录计数也不同,但是对于不同的查询,返回的计数仍然相同。 Any solution please? 有什么解决办法吗? Here is my class snippet: 这是我的课堂摘要:

<?php
    class DB {
        private static $_instance;
        private $_pdo, $_query, $_results, $_count = 0, $_errors = false;

        private function __construct() {
            try {
                $this->_pdo = new PDO('mysql:host=' . Config::info('mysql/host') . ';dbname=' . Config::info('mysql/dbname'), Config::info('mysql/dbuser'), Config::info('mysql/dbpass'));
            } catch (PDOException $e) {
                die('Failed connecting to database');
            }
        }

        public static function dbInstance() {
            if(!isset(self::$_instance)) {
                self::$_instance = new DB();
            }

            return self::$_instance;
        }

        public function query($sql = NULL, $params = array()) {
            $this->_errors = false;
            if($this->_query = $this->_pdo->prepare($sql)) {
                if(count($params)) {
                    $i = 1;
                    foreach($params AS $param) {
                        $this->_query->bindValue($i, $param);
                        $i++;
                    }
                }

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

        public function count() {
            return $this->_count;
        }
    }
?>

Adding $this->_count 0; $this->_count 0; line to the beginning of query function solved the bug for me. 行到查询功能的开头为我解决了该错误。 Now my function first sets the count to 0 and then if rows are returned, the value is changed else 0 is returned. 现在,我的函数首先将计数设置为0,然后如果返回行,则更改值,否则返回0。 Exactly what I was expecting. 正是我所期望的。

My function now looks like this: 我的函数现在看起来像这样:

<?php
        public function query($sql = NULL, $params = array()) {
            $this->_errors = false;
            $this->_count = 0;
            if($this->_query = $this->_pdo->prepare($sql)) {
                if(count($params)) {
                    $i = 1;
                    foreach($params AS $param) {
                        $this->_query->bindValue($i, $param);
                        $i++;
                    }
                }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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