public function DatabaseStatementInterface::rowCount

You are here

7 database.inc public DatabaseStatementInterface::rowCount()

Returns the number of rows affected by the last SQL statement.

Return value

The number of rows affected by the last DELETE, INSERT, or UPDATE statement executed.

2 methods override DatabaseStatementInterface::rowCount()
DatabaseStatementEmpty::rowCount in includes/database/database.inc
Returns the number of rows affected by the last SQL statement.
DatabaseStatementPrefetch::rowCount in includes/database/prefetch.inc
Returns the number of rows affected by the last SQL statement.

File

includes/database/database.inc, line 1987
Core systems for the database layer.

Class

DatabaseStatementInterface
Represents a prepared statement.

Code

public function rowCount();

Comments

Here is an example of how to use it in the context of counting the number of sub-entries for the entries of a specific 'main_menu' into a loop variable called $num (although it might be obvious for some, I'm sure this will save a few seconds to many):

$query = db_select('menu_links', 'menu');
$query->fields('menu')->condition('menu_name','main_menu')->condition('plid',0,'=');
$result = $query->execute;
while ($record = $result->fetchAssoc()) {
  $plid = $record['mlid'];
  $query_sub = db_select('menu_links', 'menu');
  $query_sub->fields('menu')->condition('menu_name', 'main_menu')->condition('plid', $plid);
  $result_sub = $query_sub->execute();
  $num = $result_sub->rowCount();
}

Can I use this rowCount with db_query?

<?php
$count_nodes
= db_query('SELECT nid FROM {node}')->rowCount();
?>

See the other DatabaseStatementInterface methods here:
http://api.drupal.org/api/drupal/includes!database!database.inc/interfac...