I’ve got a page that contains a fair few database queries, each appended with or die(). I’m loading this page every 1 second for testing, and on random page loads (it could take two, it could take five, or ten) die() is switched and an error given.
I broke down the script, and managed to isolate the particular offending query, which is:
$fetch = mysql_fetch_assoc($result) or die("Error 3:" . mysql_error());
This particular line is contained within:
if($size > 0) {
$off_id = array();
while($row = mysql_fetch_assoc($result)) {
$off_id[] = $row['off_id'];
}
echo '<pre>';
var_dump($off_id);
echo '</pre>';
$rand = rand(0,$size);
$off_id = $off_id[$rand];
$query = "UPDATE rotation_data SET hit_counter = hit_counter + 1 WHERE off_id = '{$off_id}'";
$result = mysql_query($query) or die("Error 1:" . mysql_error());
$query = "SELECT * FROM offer_data WHERE off_id = '{$off_id}'";
$result = mysql_query($query) or die("Error 2:" . mysql_error());
$fetch = mysql_fetch_assoc($result) or die("Error 3:" . mysql_error());
$offer_url = $fetch['url']; $geo_target = $fetch['geo_target']; $blank = $fetch['blank'];
}
Things I noticed:
- No
mysql_error()is returned/printed. OnlyError 3:is. - The
$off_idarray dumps correctly each and every time, so there’s always an$off_idto be used in the previous$resultquery, and if there wasn’t, that should triggerdie()for the$resultquery instead.
I don’t really understand why this would occur on random page loads, and not all the time, as this perhaps points to it not being a syntax issue, but a load issue?
However, even if it’s a load issue, I don’t understand why that particular query would fail and trigger a die() while the others are fine.
Any help in understanding why this might be, and suggestions of what I could do to fix this would be greatly appreciated!
I am guessing that your query here returns no results:
The following statement will not return
FALSE, just an empty result set into$result.And then you attempt to fetch a row from an empty result resource. This results in
FALSEnot because of error, but because there are no rows to fetch, and your short-circuit evaluation callsdie().We cannot see where you are setting
$size, but it’s possible that you are occasionally reading past the array bounds of$off_idby reaching a random value that is larger than that array.