Ryan
Ryan

Reputation: 14649

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 {

                                                   }

The if statement will not work. How can I correct this script?

Upvotes: 0

Views: 987

Answers (2)

user752897
user752897

Reputation:

@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";

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191749

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.

Upvotes: 1

Related Questions