By chandra sekhar on
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,
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
is there any functuion like mysql_num_rows ?
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.
Unfortunately Drupal
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
db_affected_rows
db_affected_rows works similar to mysql_num_rows
Similar in its purpose, but
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
db_affected_rows does not work similar to mysql_num_rows
It returns always 1, even table if table blank.
wtf why did they remove it,
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
More information...
"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
Using DISTINCT with db_num_rows() alternative
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 ...Can't make "solution" work in v.6.19
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:
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.
try this: $query = "SELECT *
try this:
$query = "SELECT * from {table}";
$result = db_query($query);
if ( $result ->num_rows > 0)
{
//contain records
}
SOLUTION
This solution works - thanks ubdr, never knew you could do that!
How to find number of rows returned?
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!