PDOStatement::rowCount
PDOStatement::rowCount — 返回受上一个 SQL 语句影响的行数(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
说明
语法
int PDOStatement::rowCount ( void )
PDOStatement::rowCount() 返回上一个由对应的 PDOStatement 对象执行DELETE、 INSERT、或 UPDATE 语句受影响的行数。
如果上一条由相关 PDOStatement 执行的 SQL 语句是一条 SELECT 语句,有些数据可能返回由此语句返回的行数。但这种方式不能保证对所有数据有效,且对于可移植的应用不应依赖于此方式。
返回值
返回行数。
实例
返回删除的行数
PDOStatement::rowCount() 返回受 DELETE、INSERT、 或 UPDATE 语句影响的行数。
<?php
/* 从 FRUIT 数据表中删除所有行 */
$del = $dbh->prepare('DELETE FROM fruit');
$del->execute();
/* 返回被删除的行数 */
print("Return number of rows that were deleted:\n");
$count = $del->rowCount();
print("Deleted $count rows.\n");
?>
以上实例输出:
Return number of rows that were deleted: Deleted 9 rows.
计算由一个 SELECT 语句返回的行数
对于大多数数据库,PDOStatement::rowCount() 不能返回受一条 SELECT 语句影响的行数。替代的方法是,使用 PDO::query() 来发出一条和原打算中的SELECT语句有相同条件表达式的 SELECT COUNT(*) 语句,然后用 PDOStatement::fetchColumn() 来取得下一行。这样应用程序才能正确执行。
<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {
/* 检查符合 SELECT 语句的行数 */
if ($res->fetchColumn() > 0) {
/* 发出一条真正的 SELECT 语句并操作返回的结果 */
$sql = "SELECT name FROM fruit WHERE calories > 100";
foreach ($conn->query($sql) as $row) {
print "Name: " . $row['NAME'] . "\n";
}
}
/* 没有匹配的行 -- 执行其他 */
else {
print "No rows matched the query.";
}
}
$res = null;
$conn = null;
?>
以上实例输出结果为:
apple banana orange pear
点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html
Game Engine Architecture, Second Edition
Jason Gregory / A K Peters/CRC Press / 2014-8-15 / USD 69.95
A 2010 CHOICE outstanding academic title, this updated book covers the theory and practice of game engine software development. It explains practical concepts and techniques used by real game studios,......一起来看看 《Game Engine Architecture, Second Edition》 这本书的介绍吧!