1

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...

6
  • 3
    You override $result in your while loop. Use a different variable. Commented May 28, 2015 at 11:00
  • This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Commented May 28, 2015 at 11:01
  • bub, thanks for reading the bottom disclaimer Commented May 28, 2015 at 11:01
  • @bub OP mentioned about it. Read it. Commented May 28, 2015 at 11:02
  • 2
    If it throws that error during the first iteration of the while loop, then it's due to no results being available (either no rows returned or a malformed query). If it does it after the first iteration, then as @JonStirling pointed out, it's because you've overwritten the $result variable that you are fetching from. Commented May 28, 2015 at 11:02

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.