从select count MySQL语句中获取行数

时间:2011-05-13 21:18:57

标签: php count

$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
                                   FROM products
                                   WHERE name LIKE '%$new_title%'
                                   GROUP BY manufacturer";


        $result2 = mysql_query($How_Many_Manufacturers, $connection) or die(mysql_error());

$num_rows = mysql_num_rows($result2);

if ($num_rows == 0)
{
    echo "<div id=\"noMatches\">No Matches</div>";
                                                   }

else {

                                                   }

if语句不起作用。 我该如何更正此脚本?

2 个答案:

答案 0 :(得分:1)

SELECT COUNT将始终返回一行(即使计数为零)。只需删除COUNT,或获取行以查看计数。

答案 1 :(得分:1)

@Arjan您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第4行-RPM附近的'-25,25'附近使用正确的语法

确保在查询中转义$ new_title。

$How_Many_Manufacturers = "SELECT COUNT(manufacturer), manufacturer
                               FROM products
                               WHERE name LIKE '%".mysql_real_escape_string($new_title)."%'
                               GROUP BY manufacturer";