0

I've looked over several SO posts, as well as articles on other websites, and have yet to find a valid answer :(.

Here's my code:

include("db_conn.php");
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error());

$timestamp = time();
$add_time = time()+(60*60);

$query = "SELECT * FROM links WHERE timestamp >= '$timestamp' AND overflow = 'NO'";
$result = mysql_query($query);

if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)){
        $link = $row['link'];
        $hit_update = rand($row['min'],$row['max']);

        $query = "UPDATE links SET timestamp = '$add_time', hit_counter = '0', max_hits = '$hit_update' WHERE link = '$link' AND timestamp <= '$timestamp' AND overflow = 'NO'";
        $result = mysql_query($query) or die(mysql_error());
}
}

mysql_close($conn);

It returns the error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

To diagnose I've tried:

  • Removing the encasing if() statement.
  • Commenting out the while() loop, and adding $size = mysql_num_rows($result); echo $size; below $result = mysql_query($query); which returns 3, i.e. there's nothing wrong with the query itself.

Does anyone know what the problem could be?

Any answers would be very much appreciated!!

Avicinnian
  • 1,822
  • 5
  • 37
  • 55

1 Answers1

3

You are overwriting the $result variable that controls the loop within the loop. That's not good. The database resource contains a cursor that you must consume fully or otherwise dispose of, just overwriting it with a new result may not get you the expected results (or indeed get you a runtime error).

Just use a different variable for the query inside the loop.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084