-1

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I am getting the following error for the code mentioned below in php:


Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Users/pubs/Sites3/parse/includes/parseAuthorSearch.php on line 62

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /Users/pubs/Sites3/parse/includes/parseAuthorSearch.php on line 62

function findExactAuthorMatch($firstname, $lastname)
{
    $query22 = "SELECT COUNT(*) FROM authors WHERE lastname='".$lastname."' AND firstname='".$firstname."'";    
    $result22 = mysql_query($query22);
    $count22 = mysql_fetch_assoc($result22);
    if($count22 == 0) return false;
    else return true;
}
Community
  • 1
  • 1
user1496783
  • 29
  • 2
  • 9
  • 1
    [I don't know why people still answer these questions](http://stackoverflow.com/search?q=%5Bphp%5D+Warning%3A+mysql_fetch_assoc%28%29+expects+parameter+1+to+be+resource%2C+boolean+given+in) – Mike B Jul 10 '12 at 20:30

4 Answers4

1

Your query is wrong, try executing it in PHPMyAdmin or other, or try to echo it and see if it seems consistent.

Jean-Marie Comets
  • 716
  • 2
  • 7
  • 21
1

It looks like your mysql_query call returned false (a boolean), and then you're calling mysql_fetch_assoc on it without checking that it was valid.

Try this:

$result22 = mysql_query($query22) or die('Invalid MySQL query: ' . mysql_error());

That will tell you where the query died.

KRyan
  • 6,784
  • 2
  • 37
  • 66
1
  • You should not be using mysql_* functions any more
  • You should never, ever, ever directly put variables into a query; see the various escape functions available for the corresponding API. In this case use mysql_real_escape_string
  • To see the error of your query, do:

    if (!$result22) { die(mysql_error()); }

mwillbanks
  • 991
  • 1
  • 8
  • 10
0

This means your previous call to mysql_query() returned false, and so there are no results to fetch. This occurs when something was wrong with the query, such as a syntax error.

In this case you will want to check if $result22 is false, then see what mysql_error() has to say:

if ($result22 === false) {
    echo mysql_error();
    return false;
}
Michael Hampton
  • 9,249
  • 4
  • 49
  • 92