1

Quick Question;

$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");

while($cronjob = mysql_fetch_array($sql)){
    if($cronjob['suid'] != $cronjob['cuid']){
        //echo 'not equal<br>';
        $set = 0;
        $sid = $cronjob['sid'];
        $suid = $cronjob['suid'];
        $cuid = $cronjob['cuid'];
        $set = notify($sid, $suid, $cuid);
        if($set==1){
            //echo 'notified<br>';
            $sql = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
            if(mysql_query($sql)){
               echo '1<br>';
              $set = 0;
          }
        }
      }
   }
}

notify() will return 1 (numeric)

The problem is only one iteration of the while loop is executed even though there are more records. I don't know what's wrong here. Help me out pls.

3
  • 1
    Is your if condition inside while loop true for multiple records? Commented May 22, 2012 at 10:53
  • @heyanshukla could be. but majority of the records wouldn't be. this just stops after processing one record. Commented May 22, 2012 at 10:57
  • then first of all echo somethihng out of if condition in while loop.and check the number of iteration. Commented May 22, 2012 at 10:59

3 Answers 3

5

Please change inner $sql variable name to something else..outer $sql and inner one are making conflict

$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");

while($cronjob = mysql_fetch_array($sql)){
    if($cronjob['suid'] != $cronjob['cuid']){
        //echo 'not equal<br>';
        $set = 0;
        $sid = $cronjob['sid'];
        $suid = $cronjob['suid'];
        $cuid = $cronjob['cuid'];
        $set = notify($sid, $suid, $cuid);
        if($set==1){
            //echo 'notified<br>';
            $sql_2 = "UPDATE cronjobs SET status = '1' WHERE id='".$cronjob['id']."'";
            if(mysql_query($sql_2)){
               echo '1<br>';
              $set = 0;
          }
        }
      }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just an observation:

Because you have:

$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");  
while($cronjob = mysql_fetch_array($sql)){ 

Its going to execute the Query EVERY SINGLE time it goes through the loop. If you have a 100 rows, its going to execute 100 times. If you do this instead, then it executes only once.

$sql = mysql_query("SELECT * FROM cronjobs WHERE status = 0 ");  
$res = mysql_fetch_array($sql);
while($cronjob = $res){ 

It wouldnt have conflicted either!

1 Comment

Its certainly not passed by ref, sir.
0

It is clearly an issue that occurs when you have the same variable for query ($query) and Result Object ($result).try different name for mysql_query() inside the WHILE Loop.

1 Comment

This is the same recommendation as the accepted answer.

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.