0

I am creating a PHP search function -

$result=mysql_query($query);
$numItems = mysql_num_rows($result); //Number of items that resulted from the query
if ($numItems == 0) {
    $query = "select * FROM $table WHERE (name LIKE '%$termSafe%' OR tags LIKE '%$termSafe%' OR search_tags LIKE '%$termSafe%' OR ingredients_1 LIKE '%$termSafe%') AND active=1 $queryOptions ORDER BY name ASC";
    $result=mysql_query($query);
    $numItems = mysql_num_rows($result);
    if ($numItems == 0) {
        return('no results');
    } else {
        return $result;
    }
} else {
    return $result;
}

When I run this, I get the following error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

IF I pull out that line $numItems - it runs fine? What am I missing?

Rizier123
  • 57,440
  • 16
  • 89
  • 140
jacob
  • 31
  • 5

1 Answers1

0

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

mysql_query returns a boolean only when false.

You should be upgrading to mysqli or PDO.

At least escape the values using mysql_real_escape_string().

You should to check for errors

$result = mysql_query($query);
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}
meda
  • 44,540
  • 14
  • 88
  • 122
  • I get the following from that then @Meda `Invalid query: Can't find FULLTEXT index matching the column list Whole query: select *, match(name,tags,search_tags,ingredients_1,directions_1) against('side') AS score FROM deifratellirecipes WHERE match(name,tags,search_tags,ingredients_1,directions_1) against('side') AND active=1` – jacob Dec 19 '14 at 22:54
  • and I am escaping the string in a portion of the search function sorry. – jacob Dec 19 '14 at 22:55
  • @jacob add the index `ALTER TABLE deifratellirecipes ADD FULLTEXT(name,tags,search_tags,ingredients_1,directions_1);` see [this question](http://stackoverflow.com/questions/9680472/cant-find-fulltext-index-matching-the-column-list-indexes-is-set) – meda Dec 19 '14 at 23:05
  • I did that and still get the same error result @Meda – jacob Dec 19 '14 at 23:10
  • I missed something - I'm sorry @Meda and that worked to solve the problem! Thank you!! – jacob Dec 19 '14 at 23:17
  • @jacob welcome, please close the question by accepting it – meda Dec 19 '14 at 23:18