Community Documentation

db_affected_rows

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

▾ 14 functions call 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.
openid_user_delete_form_submit in modules/openid/openid.pages.inc
search_index in modules/search/search.module
Update the full-text search index for a particular item.
search_update_totals in modules/search/search.module
This function is called on shutdown to ensure that search_total is always up to date (even if cron times out or otherwise fails).
sess_write in includes/session.inc
Writes an entire session to the database (internal use only).
statistics_exit in modules/statistics/statistics.module
Implementation of hook_exit().
system_update_6038 in modules/system/system.install
Ensure that "Account" is not used as a Profile category.
user_set_authmaps in modules/user/user.module
Save mappings of which external authentication module(s) authenticated a user. Maps external usernames to user ids in the users table.
variable_set in includes/bootstrap.inc
Sets a persistent variable.
_update_cache_set in modules/update/update.module
Store data in the private update status cache table.

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);
}
?>

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 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

---

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

Login or register to post comments