浅谈PDO的rowCount函数,浅谈pdorowcount
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];
显然第二种方法更有效率
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.