monxas
monxas

Reputation: 2637

mysql "boolean given" without mysql errors

I'm getting the error

connected
`Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given`

I know that means there's a problem with de sql provided, but sql is fine and I can't get it to print the error. are there other ways?

my full code:

<?php include "inc/config.php"; ?>
<?php include "inc/funciones.php"; ?>
<?php header('Content-Type: text/html; charset=UTF-8'); ?>
<?php 

if(mysql_select_db($db_db2)){
    echo "connected<br>";
}

$query = 'SELECT * FROM trabajo limit 5';
mysql_query("SET NAMES 'utf8'");
$result = mysql_query($query);

try {
    while($estado = mysql_fetch_assoc($result)){

        $query  = 'UPDATE trabajo set estadoActual=(SELECT nombreTarea FROM 
                     tareastrabajo where 
                     numeroEntrada = "'.$estado["numeroEntrada"].'" 
                     AND fechaCompletada is not null 
                     order by fechaCompletada DESC limit 1) 
                     where numeroEntrada="'.$estado["numeroEntrada"].'"';
        mysql_query("SET NAMES 'utf8'");
        $result = mysql_query($query);

    }

} catch (Exception $e) {
    echo $e->getMessage();
    echo "---";
    echo mysql_error();
}
?>

As you can see, the connection is fine, so no problems there. I also tried the classic

or die(mysql_error());

But same result.

DISCLAIMER: Please avoid answers and comments pointing out about mysqli or PDO. I inderstand the issues myself and we're in the process of migrating it. Meanwhile, we have to deal with this.

EDIT: THANKS! I WAS OVERRIDING $result. What a lame way to waste half my morning...

Upvotes: 1

Views: 79

Answers (1)

Arth
Arth

Reputation: 13110

To expand on Jon Stirling's comment you have overwritten the $result variable with the results of an UPDATE statement in the first iteration of the while loop.

Thus the error occurs on the second visit to the condition

while ($estado = mysql_fetch_assoc($result))

From the docs:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

This boolean is being passed back in to mysql_fetch_assoc() causing the exception.. there is no mysql error.

In response to Geoff Atkin's comment.. no rows returned should be handled fine by mysql_fetch_assoc()

Upvotes: 1

Related Questions