0

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

When I checked my site log error file I found this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/mysite/public_html/include/config.php on line 17

The line 17 is:

$rows=mysql_fetch_assoc($query);

And, this line is inside a function:

function get_val($val,$table,$where,$return){
    $query=mysql_query("select $val from $table $where");
    $rows=mysql_fetch_assoc($query);
    return $rows[''.$return.''];
    mysql_free_result($query);
}

There is no error in the browser but in the log_file error I find this message.

Community
  • 1
  • 1
Pin Cody
  • 135
  • 3
  • 12
  • print the query and see is it ok or not – Ariful Islam Nov 16 '11 at 06:47
  • I suspect one of your input values is invalid or blank and hence the query is not valid. btw I would strongly advise you to rethink your design. Without seeing the rest of your code, a function like this easily risks leading to bad separation in your code. If you had a field called 'price' that you later wanted to change to 'cost' you could have real problems just identifying where you need to make that change. – liquorvicar Nov 16 '11 at 08:46

2 Answers2

0

You have to check whether a resource is null or not.

function get_val($val,$table,$where,$return){
    $query=mysql_query("select $val from $table $where");
    if($query)
     {
        $rows=mysql_fetch_assoc($query);

        mysql_free_result($query);
        return $rows[$return];
     }
    return null;
}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
-1

You may also put @ instead of checking $query for null:

$query = mysql_query(...);
while ($row = @mysql_fetch_assoc($res)) {
    ....
}
Kasheftin
  • 7,509
  • 11
  • 41
  • 68
  • -1 Suppressing errors in not advised here. He's building a query dynamically and should really be capturing any errors his script is throwing. – liquorvicar Nov 16 '11 at 08:45