By bhavers on
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
Use db_result with a COUNT
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
Thx
That solved it.
Tip o the Hat
Thanks! Saved my hair from being pulled out!
-------------------------------------------
Interactive Worlds and Immersive Obsessions
http://www.asifproductions.com
Why
So we have to hit the database twice?? I don't like that.
Because db_num_rows is not
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
double dipping
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:
Try db_affected_rows().
Try db_affected_rows().
Contact me to contract me for D7 -> D10/11 migrations.
db_affected_rows()
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.
works for SELECT queries, too!
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.
good to know
This is in fact an old thread, but thanks for the update regarding db_affected_rows(). That's very good news indeed. :-)
You can try this if you use
You can try this if you use drupal with mysql
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
Exactly what i need. Thank
Exactly what i need. Thank you jaypan.
Thanks and Regards
ARUN AK
www.developersdocs.com
If you are using mysqli,
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!
That's great ! Thanks But
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
an elegant solution which i
an elegant solution which i found is using num_rows:
viewed in http://api.drupal.org/api/drupal/includes%21database.pgsql.inc/function/...