【发布时间】:2015-11-22 11:12:18
【问题描述】:
这是我的 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() 方法,而是使用将所有行作为数组获取到变量,然后计算该数组的元素 rowCountQuery 方法包含上述功能
但我现在无法获得行数???方法返回空输出
但如果我删除 (2),我可以从 rowCountQuery 方法获取并回显输出 没错
我想知道如何获取不删除的行数 (2)
【问题讨论】: