Home > Backend Development > PHP Tutorial > 浅谈PDO的rowCount函数_PHP

浅谈PDO的rowCount函数_PHP

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-30 08:44:59
Original
958 people have browsed it

PDO有一个函数PDOStatement::rowCount返回上一个SQL语句影响的行数。

rowCount函数对于DELETE, INSERT, 或者UPDATE语句的结果是正确的,但对于select语句则与数据库的实现相关。有些数据库在执行select语句时会将结果集全部读入内存,但对于数量巨大的结果集,这样显然是低效的。大部分的数据库则只会返回结果集的一部分,当需要时再返回其余的结果集,这样无论是内存占用和执行效率都是优化的。对于后一种情况,则rowCount无法返回正确的SELECT语句结果集的行数。 获取正确的SELECT结果的行数有几种方法

1、使用fetchAll函数 $q = $db->query("SELECT ..."); $rows = $q->fetchAll(); $rowCount = count($rows);
2、使用sql count函数 $q = $db->query("SELECT count(*) from db;"); $rows = $q->fetch(); $rowCount = $rows[0];

显然第二种方法更有效率

以上所述就是本文的全部内容了,希望大家能够喜欢。

Related labels:
pdo
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Database pdo connection problem
From 1970-01-01 08:00:00
0
0
0
pdo installation
From 1970-01-01 08:00:00
0
0
0
$pdo->exec($sql);Why does it always return false?
From 1970-01-01 08:00:00
0
0
0
PHP PDO predefined constants
From 1970-01-01 08:00:00
0
0
0
PDO query data error
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template