4.6.x database.pgsql.inc db_affected_rows()
4.6.x database.mysql.inc db_affected_rows()
4.7.x database.pgsql.inc db_affected_rows()
4.7.x database.mysqli.inc db_affected_rows()
4.7.x database.mysql.inc db_affected_rows()
5.x database.pgsql.inc db_affected_rows()
5.x database.mysqli.inc db_affected_rows()
5.x database.mysql.inc db_affected_rows()
6.x database.pgsql.inc db_affected_rows()
6.x database.mysqli.inc db_affected_rows()
6.x database.mysql.inc db_affected_rows()

Determine the number of rows changed by the preceding query.

Related topics

13 calls to db_affected_rows()
cache_set in includes/cache.inc
Store data in the persistent cache.
filter_list_format in modules/filter/filter.module
Retrieve a list of filters for a certain format.
lock_acquire in includes/lock.inc
Acquire (or renew) a lock, but do not block if it fails.
lock_may_be_available in includes/lock.inc
Check if lock acquired by a different process may be available.
node_type_update_nodes in modules/node/node.module
Updates all nodes of one type to be of another type.

... See full list

File

includes/database.pgsql.inc, line 237
Database interface code for PostgreSQL database servers.

Code

function db_affected_rows() {
  global $last_result;
  return empty($last_result) ? 0 : pg_affected_rows($last_result);
}

Comments

Jztinfinity’s picture

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 gives you number of rows returned by a select if the database is MySQL but not if it's PostgreSQL
  • In Drupal 6 if you are using PostgreSQL you can run pg_num_rows on the query return to get the number of rows
  • In Drupal 7 db_affected_rows will probably still work for select statements in MySQL but probably not for PostgreSQL
  • Beyond MySQL and PostgreSQL - don't expect this to work, it might, but probably not
kiamlaluno’s picture

db_affected_rows() doesn't exist in Drupal 7.

Jonah Ellison’s picture

For static queries:

$pdos = db_query("UPDATE ...");
$affected = $pdos->rowCount();
kalman.hosszu’s picture

execute() returns with affected rows:

<?php
$query = db_update('epg_files');
// conditions etc
$affected_rows = $query->execute();
?>
ufku’s picture

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) {
  ...
}