| 5 database.mysql.inc | db_affected_rows() |
| 5 database.pgsql.inc | db_affected_rows() |
| 5 database.mysqli.inc | db_affected_rows() |
| 6 database.mysql.inc | db_affected_rows() |
| 6 database.pgsql.inc | db_affected_rows() |
| 6 database.mysqli.inc | db_affected_rows() |
Determine the number of rows changed by the preceding query.
Related topics
File
- includes/
database.pgsql.inc, line 237 - Database interface code for PostgreSQL database servers.
Code
<?php
function db_affected_rows() {
global $last_result;
return empty($last_result) ? 0 : pg_affected_rows($last_result);
}
?> Login or register to post comments
Comments
mysql support
Just to clarify - db_affected_rows for select statements does work for mysql, it is a database specific issue, which is odd since PostgreSQL does have a function pg_num_rows - Though this may be because num_rows for sqlite (and other databases) is more complicated (since sqlite only gives number of returned rows from select for buffered results).
However since the return of db_query is a vendor specific result object if you are using PostgreSQL and need to utilize num_rows you can always run pg_num_rows on the result - this is a database specific operation and will not work for other databases. Also, in Drupal 7 the PDO extension is utilized (as I understand it db_query will still return a PDO Statement Object though, and while the PDO will give you the number of rows from a select statement with a mysql driver - this may not (though I haven't used PostgreSQL nor PDO with the PostgreSQL driver) work for PostgreSQL.
To summarize for Drupal 6
---
db_affected_rows() doesn't exist in Drupal 7.
Drupal 7 affected rows with PDOStatement
For static queries:
<?php$pdos = db_query("UPDATE ...");
$affected = $pdos->rowCount();
?>
execute() returns with
execute() returns with affected rows:
<?php$query = db_update('epg_files');
// conditions etc
$affected_rows = $query->execute();
?>
return value
Note that this function may return -1 on error(such as duplicate key) and is not safe to use as:
if (db_affected_rows()) {...
}
instead use:
if (db_affected_rows() > 0) {...
}