开发者

Get number of rows from a select count MySQL statement

$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 {

                                                   }

T开发者_C百科he if statement will not work. How can I correct this script?


@Arjan You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-25,25' at line 4 -RPM

make sure you escape $new_title in the query.

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


SELECT COUNT will always return a row (even if the count is zero). Simply remove the COUNT, or fetch the row to see the count.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜