-1

I am getting a "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given" with the following code:

$field_sql = 'SHOW FIELDS FROM '.$dbtable.' WHERE FIELD = '.$field;
$field_q = mysql_query($field_sql,$MJCONN);
$field_r = mysql_fetch_assoc($field_q);
}while($field_r = mysql_fetch_assoc($field_q));

I think it has to do with brackets but I have not been able to solve the problem, any help would be very welcome.

  • Echo out your query and run it against the database. Your query isn't working for some reason. – andrewsi Sep 11 '12 at 19:33
  • That means your query failed. You can use `mysql_error()` to find out why. – honyovk Sep 11 '12 at 19:33
  • 2
    You should probably also look at moving to `PDO` or `mysqli_*`, as `mysql_*` functions are being deprecated. – andrewsi Sep 11 '12 at 19:36
  • 1
    @user1658413: Please read my recent post about SQL injections, you could find it interesting: http://stackoverflow.com/questions/11939226/sql-injections-and-adodb-library-general-php-website-security-with-examples – Ilia Ross Sep 11 '12 at 19:36
  • `mysql_query($field_sql,$MJCONN);` returned `FALSE` - check why! – Nir Alfasi Sep 11 '12 at 19:37

3 Answers3

2

mysql_query returns false on failure of the query. I like to use this instead of mysql_query:

function mysql_query2($sql,$conn=null) {
    $r = $conn ? mysql_query($sql,$conn) : mysql_query($sql);
    if( $e = mysql_error()) trigger_error("MySQL error: ".$e,E_USER_WARNING);
    return $r;
}

This performs the query, then checks for errors and makes them PHP warnings. This way, you can easily see if the query fails.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

I fixed it myself, it was a question of missing characters, in this case quotes.

Solution:

$field_sql = 'SHOW FIELDS FROM `' . $table . '` WHERE FIELD = "' . $column . '"';
0

$field_q is FALSE, because your query failed. See documentation. Fix your query to get rid of the issue :)

rodion
  • 6,087
  • 4
  • 24
  • 29