September 8th, 2000, 01:27 PM
I'm a newbie and need some help. Basically, I want to count the number of names in a field to a web page. I use the following line to gather the data and store to a variable:
$tally=mysql_query("SELECT count(*) FROM fname",$db);
I use the next line to post it:
The output on the web page should be the number of rows counted in the field but I get:
Resource id #2
Any help is appreciated. Tx.
September 8th, 2000, 01:55 PM
I solved the problem using a different approach. Here's the way I did it:
$tally=mysql_query("SELECT * FROM fname",$db);
echo "The total number of entries is: ".mysql_num_rows($tally)."n";
Worked like a charm. It pays to read the past forum entries. Tx.
September 9th, 2000, 02:08 AM
You could use count(*), but you have to use it like this.
$tally = mysql_query("SELECT count(*) as the_count from fname",$db);
$row = mysql_fetch_row($tally);
echo "Total is " . $row["the_count"];
you have to give the count(*) a name to refer to it in the php statements...
For what you want, just a count of how many rows are in the database, how you do it is fine. but say you wanted to find out how many last names in the database start with H??
$total = mysql_query("SELECT count(last_name) AS h_names FROM database WHERE last_name LIKE 'H%'",$db);
$row = mysql_fetch_row($total);
echo "Total is " . $row["h_names"];
My syntax may be a little rusty, but you get the idea of why count(XXX) as YYY is very helpful, right??
September 12th, 2000, 04:00 PM
Thanks for your solution. As a matter of fact, I was planning on narrowing down the row count to specific names and your input will help me cut down on the amount of code necessary to acheive the result.