PDO类 - 在初始提取后获取结果

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

标签: 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 execute($array=null)
    {
        $this->stmt->execute($array);
    }

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

    public function fetchQuery()//------------(2)
    {
        return $this->query->fetch(PDO::FETCH_ASSOC);
    }

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

}

$database = new Database('localhost','root','','speed');

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


while($result2 = $database->fetchQuery())//----------calling method (2)
{
    echo $result2['area'];
    echo '<br>';
}
  • 在调用(1)方法后,我无法得到结果调用(2)方法
  • 并且在调用(2)方法之后我无法得到结果调用(1)方法

如何同时使用(1)和(2)方法获得结果?

0 个答案:

没有答案