I'm trying to convert a drupal 5 module to drupal 6 but i get the following message:
Fatal error: Call to undefined function db_num_rows() in file.module on line 440

What is the equivalent of the function db_num_rows() in drupal 6? And where do i find documentation about it?

Thanks,
Bram.

Comments

pobster’s picture

Use db_result with a COUNT query, it's documented somewhere. I think I followed a link from using the coder module.

$list_length = db_num_rows(db_query('SELECT id FROM {tablemanager_data} WHERE tid = %d', $tid));

Becomes;

$list_length = db_result(db_query('SELECT COUNT(id) FROM {tablemanager_data} WHERE tid = %d', $tid));

Pobster
edit: Found this link; http://drupal.org/node/114774#db-num-rows

bhavers’s picture

That solved it.

As If’s picture

Thanks! Saved my hair from being pulled out!

-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com

sunnydeveloper’s picture

So we have to hit the database twice?? I don't like that.

coreyp_1’s picture

Because db_num_rows is not standard SQL and is not supported in the same way across different database engines.

You can see the thread where the deed was done here.

-Corey

johnhanley’s picture

I don't like the idea of hitting the database twice either... not sure why some devs find that acceptable.

I prefer (whenever possible) to use the query result to determine if a recordset contains any rows.

As for the example shown on the db_num_rows conversion example, the $num_rows flag is unnecessary and the empty $output string serves just as well:

<?php
function taxonomy_render_nodes($result) {
  $output = '';
  while ($node = db_fetch_object($result)) {
    $output .= node_view(node_load($node->nid), 1);
  }
  if ($output) {
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
  }
  else {
    $output .= '<p>'. t('There are currently no posts in this category.') .'</p>';
  }
  return $output;
}
?>
jaypan’s picture

Contact me to contract me for D7 -> D10/11 migrations.

johnhanley’s picture

db_affected_rows() returns the number of rows affected for INSERT, UPDATE, and DELETE queries and only returns TRUE for SELECT queries when the result contains one or more rows.

tbisciglia’s picture

It was very likely true in 2009 that db_affected_rows() did not return correct results for SELECT queries. However, it does now! I use this routinely to find the number of rows returned by a SELECT query to determine how to proceed. It's really useful, and I don't have to run separate queries.

I highly recommend the use of db_affected_rows() in Drupal 6 to catch possible data errors (missing data or duplicate data) in your queries, without having to run a separate count(*) query.

johnhanley’s picture

This is in fact an old thread, but thanks for the update regarding db_affected_rows(). That's very good news indeed. :-)

-enzo-’s picture

You can try this if you use drupal with mysql

 $results = db_query($query);

 $rows = mysql_num_rows($results);

Enjoy it.

enzo

--
enzo - Eduardo Garcia
weKnow - http://www.weknowinc.com
Please use the git author option: --author="enzo " for any patch I did and used in a new module release

arun ak’s picture

Exactly what i need. Thank you jaypan.

Thanks and Regards
ARUN AK
www.developersdocs.com

ZMan9854’s picture

If you are using mysqli, since db_query returns a mysqli object, you can just use $result->num_rows. This will differ for whatever database you're using though!

timos’s picture

That's great !
Thanks
But it's true, it would be nice to have an generic method in D6 to do that.
What about D7 ?

Tim Baret

alexmoreno’s picture

an elegant solution which i found is using num_rows:

$query = db_query($sql,$params);
$query->num_rows

viewed in http://api.drupal.org/api/drupal/includes%21database.pgsql.inc/function/...