Wrong parameter type error in mysql_fetch_assoc()

Asked

Viewed 233 times

0

Warning: mysql_fetch_assoc() expects Parameter 1 to be Resource, Boolean Given in C: xampp htdocs NEW admin alterarfotos.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: id_imagem in C: xampp htdocs NEW admin alterarfotos.php on line 15

I am creating a simple album with Mysql & PHP but I am having this error.

1 answer

3


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

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

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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.