0

Here, Is the code in this code if we remove the query then loop worked fine and if we add the Select and Insert Mysql Query then loop is running only once what is the issue in it.

for($credit=0;$credit<count($execQuery);$credit++){
        
        $feeHeadValue = 0;
        $creditAmount = $execQuery[$credit]['creditAmount'];
        $query = "select
        feeHeadAmount,
        feeHeadId
        from
        fee_head_values_student
        where
        studentId = ?
        and feeHeadId = ?
        and feeCycleId = ?";
        $queryArray = array($studentId,$execQuery[$credit]['feeHeadId'],$execQuery[$credit]['feeCycleId']);
        $execQuery1 = $this->db->query($query,$queryArray)->result_array();
        // echo"<pre>";print_r($execQuery1);
        $feeHeadValue = isset($execQuery1[$credit]['feeHeadAmount']);

       

        if(isset($feeHeadValue)){
        $query ="
        update 
        fee_head_student_wise
        set 
        feeHeadAmount = if((feeHeadAmount + $creditAmount)<=$feeHeadValue,(feeHeadAmount + $creditAmount),feeHeadAmount)
        where 
        studentId = ?
        and feeHeadId = ?
        and feeCycleId = ?
        ";
        $queryArr = array($studentId,$execQuery[$credit]['feeHeadId'],$execQuery[$credit]['feeCycleId']);   
        $execQuery  = $this->db->query($query,$queryArr);
     }
        echo $credit;
    }

if i remove the Update queries then loop worked fine and if we add Update query then loop execute only once.

David
  • 208,112
  • 36
  • 198
  • 279
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Apr 11 '23 at 13:21
  • You do `$feeHeadValue = isset($execQuery1[$credit]['feeHeadAmount']);`, then you check `if(isset($feeHeadValue)){`. `$feeHeadValue` is already set, so it will always hit that `if` check. You probably want just `if($feeHeadValue)` – aynber Apr 11 '23 at 13:23
  • if i removed if(isset($feeHeadValue)){} from update query then it is also not working. – Saurabh Banga Apr 11 '23 at 13:30
  • 3
    So what _exactly_ does "then loop execute only once" mean - does it only perform one loop iteration, and then continues with the rest of the script; or does it perform one loop iteration, and then somehow "dies" at that point? Do you have proper PHP error reporting enabled to begin with? If not, please go and do that first of all. – CBroe Apr 11 '23 at 13:35

0 Answers0