tags:

views:

210

answers:

3

I am using C MySQL API

int numr=mysql_num_rows(res);

It always returns zero, but in my table there are 4 rows are there. However, I am getting the correct fields count.

what is the problem? Am i doing anything wrong?

+2  A: 

The only reason to receive a zero from mysql_num_rows(<variable_name>) is because the query did not return anything.

You haven't posted the query here that you run and then assign the result to your res variable so we can't check it.

But try running that exact query in your DB locally through whatever DB management software you use and see if you are able to achieve any results.

If the query is working fine, then it must be the way you're running the query in C, otherwise your query is broken.

Maybe post up a bit more of your code from C where you make the query and then run it.

Thanks

mlevit
Upvoted. It sounds like his query has a where clause which is not matching anything or is broken.
Sean A.O. Harney
+3  A: 

Just a guess:

If you use mysql_use_result(), mysql_num_rows() does not return the correct value until all the rows in the result set have been retrieved.

(from the mysql manual)

cube
If you need the number of rows before calling mysql_fetch_row(), use mysql_store_result() instead of mysql_use_result().
chazomaticus
A: 

If you just want to count the number of rows in a table, say

SELECT COUNT(*) FROM table_name

You will get back a single column in a single row containing the answer.

Warren Young