Wrong parameter type error in mysql_fetch_assoc ()

0
  

Warning: mysql_fetch_assoc () expects parameter 1 to be resource, boolean given in C: \ xampp \ htdocs \ NEW \ admin \ alterfotos.php on line 16

$id_imagem = trim($_GET['id_imagem']);
   $dados = mysql_fetch_assoc(       mysql_query("SELECT * FROM imagens WHERE id_imagem = $id_imagem")      )or die(mysql_error());
  

Notice: Undefined index: image_id in C: \ xampp \ htdocs \ NEW \ admin \ alterphotos.php on line 15

I'm creating a simple album with MySQL & PHP however I'm having this error.

    
asked by anonymous 18.09.2014 / 01:56

1 answer

3

According to the PHP documentation , the mysql_fetch_assoc function expects that the parameter passed is of type resource .

However you are directly passing the return of the mysql_query function as a parameter, which, according to a documentation will return false if an error occurs in the query execution.

You should separate your code as follows:

$id_imagem = trim($_GET['id_imagem']);

$query = mysql_query("SELECT * FROM imagens WHERE id_imagem = $id_imagem") or die(mysql_error());
$dados = mysql_fetch_assoc($query);
    
18.09.2014 / 02:10