Forum Moderators: coopster & phranque

Message Too Old, No Replies

mysql_num_rows problem

         

ukgimp

2:25 pm on Jun 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is probably quite an easy problem, but it has me baffled. I have a query and I am trying to do next previous links etc at the bottom so a user can scroll through.

Here is a little snippet

$query_count = "SELECT COUNT(*) FROM table"; //method 1
$query_count = "SELECT ID FROM table"; //method 2
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
echo $totalrows;

Now if I use method 1 the figure returnd it 1, but with method 2 it gives me the right answer (239)

What is going on, i would prefer method one as it does not extract the data, just counts it.

Cheers

mole

2:32 pm on Jun 12, 2003 (gmt 0)

10+ Year Member



it's 'cos in method 1 you're getting a result set with just one row in it.
The first element in that row is actually the number of records matching your search criteria, but that ain't what you're outputting.

In method 2 of course, you get the number of records matching the search criteria. Each row in the results set contains an ID

DrDoc

3:20 pm on Jun 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to use COUNT(), this is what you need to do:

$query_count = "SELECT COUNT(ID) FROM table";
$result_count = mysql_query($query_count);
$get = mysql_fetch_row($result_count);
echo $get[0];