0

New to this forum and php so please go easy on me.

I have a php script that is dealing with data from multiple mysql tables in a database. I have all of the other queries working fine, but for some reason this one throws an error whenever the conditions are true to reach the mysql_fetch_array() command. I've been trying to find how this query differs from all the others, but have not had any success.

Does anyone see anything in the following segment of code that could lead to the following error?

$errorFlag = 0;
$result = 0;
$counter = 0;
$sql = "SELECT * FROM Data WHERE Last_Update < DATE_ADD(NOW(), interval -1 MINUTE)";
while (!$result && $errorFlag == 0) {
  $result = mysql_query($sql);
  $counter = $counter + 1;
  if ($counter > 30) {
    $errorFlag = 1;
  }
}
if ($errorFlag == 1) {
  echo "#E01,".$counter.",".mysql_errno().":".mysql_error()."!";
}
if ($errorFlag == 0 && mysql_num_rows($result) > 0) {
  while(errorFlag == 0 && $row = mysql_fetch_array($result)) {
    if ($row['Last_Update_Alarm'] == 0) {
      $sql = "UPDATE $tbl_name SET Last_Update_Alarm='1', Status_Time='0' WHERE Machine_ID='$row[Machine_ID]'";
      $counter = 0;
      $result = 0;
      while ($result == 0  && $errorFlag == 0) {
        $result = mysql_query($sql);
        $counter = $counter + 1;
        if ($counter > 30) {
          $errorFlag = 1;
        }
      }
    }
  }
}

This is the error code that I get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:...\test.php on line 33

Line 33 corresponds to the line with fetch command. I checked the resource type and the number of rows after the query and both were as expected.

Thanks! Scott

  • Hint: look at `$result` in line 33 and see if it's being set correctly in the prior lines (it's probably not, check your first `while` loop (which seems unnecessary)). –  Sep 05 '13 at 20:45
  • Oh and see [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) –  Sep 05 '13 at 20:46
  • Please, before you write **any** more SQL interfacing code, you must read up on [proper SQL escaping](http://bobby-tables.com/php) to avoid severe [SQL injection bugs](http://bobby-tables.com/). Also, `mysql_query` should not be used in new applications. It's a deprecated interface that's being removed from future versions of PHP. A modern replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and will make your database code easier to get right. – tadman Sep 05 '13 at 20:48
  • Thanks for the responses so far. That's a lot to cover, so I will get back to you once I get through it all. – user2751924 Sep 05 '13 at 21:02

0 Answers0