Hai All,

I am new to drupal.
I want to know is there any alternative function for mysql_num_rows in drupal ?
if anythig is there please tell me

thanks,

Comments

rightchoice2c_me’s picture

you can use db_result to find number of rows results have.
ex:
db_result(db_query("select count(*) from users"));
it will return number of users.

Work Hard to understand what you have to do b4 U start, You may not be able to develop every detail but the more U know the less risk you take.

yuriy.babenko’s picture

Unfortunately Drupal developers removed it from 6.x (in 5.x there is db_num_rows()) in favour of the solution posted above, which IMO, can be a considerable inconvenience, and why I sometimes simply use mysql_num_rows().
---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

ramya1585’s picture

db_affected_rows works similar to mysql_num_rows

yuriy.babenko’s picture

Similar in its purpose, but it does something quite different.

db_num_rows() returns the number of rows in your SELECT or SHOW query.
db_affected_rows() returns the number of rows affected by your INSERT/UPDATE/REPLACE/DELETE query.

---
Yuriy Babenko
www.yubastudios.com
My Drupal tutorials: http://yubastudios.com/blog/tag/tutorials

---
Yuriy Babenko | Technical Consultant & Senior Developer
http://yuriybabenko.com

bmkaradia’s picture

It returns always 1, even table if table blank.

Michsk’s picture

wtf why did they remove it, num_rows is the best way to check if there is data in your query without getting data out of it

NoCoolNamesRemain’s picture

"The db_num_rows() method was removed from the database abstraction layer in 6.x core, as it was a database dependent method. Developers need to use other handling to replace the needs of this method."

Two alternatives can be found at http://drupal.org/node/114774#db-num-rows

NoCoolNamesRemain’s picture

If you original query looked something like...
SELECT DISTINCT example_column FROM ...
..your new query would need to be...
SELECT COUNT(DISTINCT(example_column)) FROM ...

Stupidscript’s picture

I am in dire need of assistance with the db_result(db_query()) "solution" to the extremely-useful-and-now-gone mysql_num_rows.

First of all, it's a drain to need to perform TWO queries just to get the row count for ONE query, and secondly, the solution only seems to work when the query is exceedingly simple, i.e. SELECT COUNT(*) from yadda.

Lastly, I have not been able to discover where or how to establish my own function to re-establish mysql_num_rows(). See my thread regarding that attempt, here: http://drupal.org/node/955042

For example, here is a moderate query ... not even very complex ... for which I simply cannot get a count of the number of rows returned. I MUST run an even more complex query because in use this will be filtered and the number of results per page limited by user selections, so I can't just do a "select all" simple query as all of the mysql_num_rows replacement examples seem to use, and I can't even use the SAME query that produces the on-screen data because of the LIMIT query parameter in the "real" query that displays results to the user:

"SELECT DISTINCT(users.uid) AS uid_1,
users.uid AS uid,
profile_values_profile_name.value AS profile_values_profile_name_value,
profile_values_profile_name.uid AS profile_values_profile_name_uid,
profile_values_profile_address.value AS profile_values_profile_address_value,
profile_values_profile_city.value AS profile_values_profile_city_value,
profile_values_profile_state.value AS profile_values_profile_state_value,
profile_values_profile_zip.value AS profile_values_profile_zip_value,
profile_values_profile_description.value AS profile_values_profile_description_value,
FROM users users
LEFT JOIN profile_values profile_values_profile_name ON users.uid = profile_values_profile_name.uid AND profile_values_profile_name.fid = '12'
LEFT JOIN profile_values profile_values_profile_address ON users.uid = profile_values_profile_address.uid AND profile_values_profile_address.fid = '14'
LEFT JOIN profile_values profile_values_profile_city ON users.uid = profile_values_profile_city.uid AND profile_values_profile_city.fid = '15'
LEFT JOIN profile_values profile_values_profile_state ON users.uid = profile_values_profile_state.uid AND profile_values_profile_state.fid = '16'
LEFT JOIN profile_values profile_values_profile_zip ON users.uid = profile_values_profile_zip.uid AND profile_values_profile_zip.fid = '17'
LEFT JOIN profile_values profile_values_profile_description ON users.uid = profile_values_profile_description.uid AND profile_values_profile_description.fid = '20'
WHERE LENGTH((profile_values_profile_name.value)) > 0
GROUP BY uid_1";

Using "SELECT COUNT(DISTINCT(users.uid) as uid_1)..." as the query and db_result(db_query($query)) returns nothing ... no errors, no data.

I am seeking any help getting SOME form of mysql_num_rows working with a similar query, even if I have to make my own replacement function, if I can find out WHERE it goes in order to work, as noted in the above listed thread.

I simply want SOMETHING that works JUST LIKE mysql_num_rows() does. I'll even suffer with running two queries, just to get the count, if that's the ONLY way Drupal can handle it.

Can anyone help me out, please? Thanks in advance.

ubdr’s picture

try this:
$query = "SELECT * from {table}";
$result = db_query($query);
if ( $result ->num_rows > 0)
{
//contain records
}

willeaton’s picture

This solution works - thanks ubdr, never knew you could do that!

Mohal’s picture

Hi!

I have been trying to work in Drupal for last 13 months or so... but I still don't get it why there is no help available.

I regularly stuck at each & every step of the program. And there is no solution available anywhere.

Can anyone tell me how to find the number of rows returned by the query?

It will be a great help for me.

Regards!