如何在PDO中使用FOUND_ROWS()函数?

时间:2017-08-03 09:22:34

标签: php sql pdo mariadb

当我使用mysql_query时,我使用了FOUND_ROWS()这样的函数:

$query = 'SELECT SQL_CALC_FOUND_ROWS * FROM Users';
mysql_query($query);
$query = 'SELECT FOUND_ROWS()';
mysql_query($query);

现在我使用PDO,这是我的查询:

$sth = $this->dbh->prepare("SELECT SQL_CALC_FOUND_ROWS *,
                                   u.id user_id,
                                   u.avatar,
                                   u.date_time,
                                   CONCAT(u.user_fname, ' ', u.user_lname) name,
                                   sum($this->table_alias.vote_value) vote_value,
                                   sum($this->table_alias.score) score,
                                   $category_in_the_select AS tc $tag_in_the_select AS tt
                            FROM users u
                            JOIN reputations $this->table_alias ON u.id = $this->table_alias.owner_id $query_join
                            WHERE $time_limitation $query_where
                            GROUP BY user_id, u.avatar, u.date_time, name, tc, tt
                            ORDER BY score DESC, vote_value DESC
                            LIMIT :j, $this->per_page");
$this->parameters[":j"] = $j;
$sth->execute($this->parameters);
$users = $sth->fetchAll(PDO::FETCH_ASSOC);

如您所见,我在SQL_CALC_FOUND_ROWS声明中使用了SELECT,我想知道如何使用FOUND_ROWS()

1 个答案:

答案 0 :(得分:0)

您需要做的就是在原始执行后运行->query('SELECT FOUND_ROWS()')

$sth = $this->dbh->prepare("SELECT SQL_CALC_FOUND_ROWS *,
                                   u.id user_id,
                                   u.avatar,
                                   u.date_time,
                                   CONCAT(u.user_fname, ' ', u.user_lname) name,
                                   sum($this->table_alias.vote_value) vote_value,
                                   sum($this->table_alias.score) score,
                                   $category_in_the_select AS tc $tag_in_the_select AS tt
                            FROM users u
                            JOIN reputations $this->table_alias ON u.id = $this->table_alias.owner_id $query_join
                            WHERE $time_limitation $query_where
                            GROUP BY user_id, u.avatar, u.date_time, name, tc, tt
                            ORDER BY score DESC, vote_value DESC
                            LIMIT :j, $this->per_page");
$this->parameters[":j"] = $j;
$sth->execute($this->parameters);
$users = $sth->fetchAll(PDO::FETCH_ASSOC);

$count = $this->dbh->query('SELECT FOUND_ROWS()')->fetchColumn();