如何将COUNT的结果放入变量

时间:2015-04-18 20:38:35

标签: php mysql

我发现了这个问题Count number of times value appears in column in MySQL,我希望能够与之相提并论。我想将查询结果放入变量中。我试过了

$sql=mysql_query("SELECT COUNT( * ) FROM myTable WHERE town = 'Sydney'");
echo $sql;

但这会回应整个命令,而不是结果(应该是2) 关于这个的任何想法?

1 个答案:

答案 0 :(得分:-1)

您可以使用mysql_num_rows()。这样的事情。

$sql=mysql_query("SELECT * FROM myTable WHERE town = 'Sydney'");
$count = mysql_num_rows($sql);
echo $count;

mysql_num_rows - 获取结果中的行数

OR与COUNT()mysql函数

$sql = mysql_query("SELECT COUNT(*) as count FROM myTable WHERE town = 'Sydney'");
while($rw=mysql_fetch_object($sql)){
    echo $count = $rw->count;
    }