using PHP (running on Apache/Linux) I have used the following script to find the number of rows in the User Database.


$Link = mysql_connect("DB_HOST", "DB_USER", "DB_PASS")
or die (mysql_errno().": ".mysql_error());
mysql_select_db ("DB_TABLE", $Link)
or die (mysql_errno().": ".mysql_error());

$Query = "SELECT Count(*) FROM Users";
$Results = mysql_query($Query, $Link)
or die (mysql_errno().": ".mysql_error());

echo $Results;


Problem is this echos "Resource id #2" and not the count. How do I get the count? Thanks.

    you have to use the mysql_fetch_array, mysql_fetch_row, or mysql_result to first get the data out of the resultset

      You are correct, I can fetch a variable from the Table and then count the number of row found in the results.

      However, If I don't need any data from any of the columns in the table, but I still want a count, then it is faster and uses alot less memory to just query the count.

      In the mysql daemon on my linux server, at the mysql prompt I enter:

      SELECT Count(*) FROM Users;

      and this returns just the count (example 328).

      I don't need any other data from the table, I don't need user name, address, etc. Just the number of users. Since I don't need all this data it is just faster to not query for it, and just query the count.

      I was just wondering if there is a way in PHP to get the raw output from a mysql query (as shown above) and put that in a variable (called Count) without needing to fetch an array of column data that I won't use anyway.

      Much thanks.

        Write a Reply...