I found this question Count number of times value appears in column in MySQL and I want to tie up to it. I want to put the result of the query into a variable. I tried
$sql=mysql_query("SELECT COUNT( * ) FROM myTable WHERE town = 'Sydney'");
echo $sql;
But this will echo whole command, not the result (which should be 2) Any ideas on this?
You can use mysql_num_rows(). Something like this.
$sql=mysql_query("SELECT * FROM myTable WHERE town = 'Sydney'");
$count = mysql_num_rows($sql);
echo $count;
mysql_num_rows — Get number of rows in result
OR with COUNT() mysql function
$sql = mysql_query("SELECT COUNT(*) as count FROM myTable WHERE town = 'Sydney'");
while($rw=mysql_fetch_object($sql)){
echo $count = $rw->count;
}