Forum Moderators: coopster

Message Too Old, No Replies

mysql num rows problem?

         

shams

1:52 am on Dec 2, 2006 (gmt 0)

10+ Year Member



hi,
this code should give the total number of rows from table pharmacy:
<?php
// Make a MySQL Connection
// Connects to your Database
include 'library/config.php';
include 'library/opendb.php';

$query_count = "SELECT count(*) FROM pharmacy";
// Sets what we want to pull from the database
// count(*) is better for large databases (thanks Greg!)

$result_count = mysql_query($query_count);
// Pulls what we want from the database

$totalrows = mysql_num_rows($result_count);
echo "totalrows = ".$totalrows;
include 'library/closedb.php';
?>
but this is the output:
totalrows = 1
but there are 3 rows in this table, this is the query from mysql prompt:
mysql> SELECT count(*) FROM pharmacy;
+----------+
¦ count(*) ¦
+----------+
¦ 3 ¦
+----------+
1 row in set (0.00 sec)
any one can help please?

eelixduppy

6:15 am on Dec 2, 2006 (gmt 0)



Try this:

$query_count = "SELECT count(*) as `Num` FROM pharmacy";
$result_count = mysql_query($query_count);
$row = mysql_fetch_array($result_count);
$totalrows = $row['Num'];
echo 'totalrows = '.$totalrows;

The reason it was '1' in your example is shown by the query that you did. It returns '3', which is '1' row. If you are selecting the count in the query then there is no need to count the results rows, and actually it's not correct. All you want if the info that is returned from mysql.

Good luck!