Rehmat
Rehmat

Reputation: 5071

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;
        }
    }
?>

Upvotes: 0

Views: 34

Answers (1)

Rehmat
Rehmat

Reputation: 5071

Adding $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. 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;
        }
?>

Upvotes: 1

Related Questions