获取pdo类中的行数 - 获取数据后

时间:2015-11-22 11:12:19

标签: php mysql pdo

这是我的pdo课程

<?php
class Database
{
    protected $dbh;
    protected $query;
    public $rows;

    public function __construct($host,$user,$pass,$dbname)
    {
        // Set DSN
        $dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;

        // Set options
        $options = array(
                            PDO::ATTR_PERSISTENT    => true,
                            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
                        );
        // Create a new PDO instanace
        try
        {
            $this->dbh = new PDO($dsn, $user, $pass, $options);
        }
        catch(PDOException $e)
        {
            echo $this->error = $e->getMessage();
            die();
        }
    }

    public function query($query)
    {
        $this->query = $this->dbh->query($query);
    }

    public function fetchAllQuery()
    {
       return $this->query->fetchAll(PDO::FETCH_ASSOC);
    }

    public function rowCountQuery()//--------------------------------(1)
    {
        //for get all the rows when query execute
        $arr = $this->fetchAllQuery()
        $this->rows = count($arr);
    }


}

$database = new Database('localhost','root','','speed');
$area='aa';
$price=200;


$query  = $database->query("SELECT * FROM tbl_deliverygrid"); 
$result = $database->fetchAllQuery();///-------------------------------(2)
echo '<pre>',print_r($result),'</pre><br>';



echo 'row count query [['.count($result).']]<br>';
$database->rowCountQuery();//---------------calling (1)
echo '['.print_r($database->rows).']';
echo '<br>';

我想获得查询的行数

为了代替使用rowCount()方法,我使用fetch所有行作为数组变量,然后计算该数组的元素 rowCountQuery方法包含上述功能

但我现在不能获得行数???方法返回null输出

但如果我删除(2)我可以得到并回显rowCountQuery方法的输出 corrrectly

我想知道如何获取行数而不是删除(2)

2 个答案:

答案 0 :(得分:1)

您在同一个脚本中连续两次提取结果集,这将导致第二个请求的null结果。由于您已经在(2)之后得到了结果集,因此可以在count()上执行$result

答案 1 :(得分:1)

<?php
class Database
{
    protected $dbh;
    protected $query;
    public $rows;
    protected $count;

    public function __construct($host,$user,$pass,$dbname)
    {
        // Set DSN
        $dsn = 'mysql:host=' . $host . ';dbname=' . $dbname;

        // Set options
        $options = array(
                            PDO::ATTR_PERSISTENT    => true,
                            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
                        );
        // Create a new PDO instanace
        try
        {
            $this->dbh = new PDO($dsn, $user, $pass, $options);
        }
        catch(PDOException $e)
        {
            echo $this->error = $e->getMessage();
            die();
        }
    }

    public function query($query)
    {
        $this->query = $this->dbh->query($query);
    }

    public function fetchAllQuery()
    {
       $result = $this->query->fetchAll(PDO::FETCH_ASSOC);
       $this->count = count($result);
       return $result;
    }

    public function rowCountQuery()//--------------------------------(1)
    {
        return $this->count;
    }


}

$database = new Database('localhost','root','','speed');
$area='aa';
$price=200;


$query  = $database->query("SELECT * FROM tbl_deliverygrid"); 
$result = $database->fetchAllQuery();///-------------------------------(2)
echo '<pre>',print_r($result),'</pre><br>';



echo 'row count query [['.count($result).']]<br>';
echo '['.print_r($database->rowCountQuery()).']';
echo '<br>';