By jng12 on
I ran a db_fetch_object on an SQL query. but now I want to know the number of rows I got before doing any processing per row.
what is the best way to do that? what drupal function is there that does this (if any)?
thanks,
I ran a db_fetch_object on an SQL query. but now I want to know the number of rows I got before doing any processing per row.
what is the best way to do that? what drupal function is there that does this (if any)?
thanks,
Comments
Easy!
http://api.drupal.org/api/function/db_num_rows/5 and http://api.drupal.org/api/function/db_affected_rows/5 will be what you're after sir :-)
--
Ixis (UK): Drupal consultancy, Drupal hosting.
--
Ixis (UK): Drupal support, Drupal hosting.
perfect!
thanks!
db_affected_rows worked great!
db_num_rows is gone on version 6 so I really didn't have a choice among the two. hehe. THANKS!
Sorry, didn't notice the
Sorry, didn't notice the Drupal 6.x tag in the top right corner of your post :)
--
Ixis (UK): Drupal consultancy, Drupal hosting.
--
Ixis (UK): Drupal support, Drupal hosting.
db_result()
The most common way to get a number in D6 is something like:
Iirc, the reason num_rows() was removed was because it might caused portability and abstraction issues.
I figured I could do this,
I figured I could do this, but because I already have a query returning the rows, wouldn't that be just doubling the query unnecessarily?
Hi j.somers Can you please
Hi j.somers
Can you please tell me how to count number of rows with respect to specific column?
I wrote like this but it didnt work :
$count = db_result(db_query("SELECT COUNT(xyz) FROM {mytable} WHERE pageno=%d",$nid));
Is there any other way?
no num_rows() in D6?
*** for D6 ***
so if you're just SELECTing, you can't get the number of rows that resouce contains? You HAVE to do another query (with count())?
I just do this: $result =
I just do this:
$result = db_query($sql);
$count = count($result);
PHP will count how many sections are in the array(non_recursive, you can set recursive if you need it, but you don't for this)
http://php.net/manual/en/function.count.php
db_query() is supposed to
db_query() is supposed to return a resource, not an array.
Did you mean this ?
$result = db_query($sql);
$count = count(db_fetch_array($result));
But this returns the number of fields the query returns !
The best way is using db_num_rows/mysql_num_rows or db_result.
Drupal 7
Hi!
What if we are developing in Drupal 7?
db_result, db_num_rows & likes are not available in Drupal 7.
Thanx in advance!
Regards!
Drupal 7 Solution!
Hi!
First of all Drupal must define a function similar to "mysql_num_rows," secondly if someone is looking to do this in Drupal 7 then the easiest method is to execute same query twice.
E.g.
If there is still some kind of confusion then you can ask from me. Drupal is very difficult and the problem is is that there are no easy examples given. From my experience if you want to learn Drupal then observe the default functions of Drupal; how they have implemented the stuff you want to implement.
Regards!
_
Actually, for >d7 there's rowCount() :
See http://drupal.org/node/1251174 for more info.
Its working fine
rowCount() is working fine. Thanks WorldFallz :)
Ya.... This is also working
Ya.... This is also working for me...
Thanks