Orlo
Orlo

Reputation: 828

While loop stops after first row

I've a problem with my while loop. instead of looping through each row, it executes once and exit the loop.

the loop:

while($row = $db->get_array()){
    ...
    $db->query("DELETE FROM mytable WHERE id='{$row['id']}'");
}

It exits only with my $db class if I run echo $row['id']}; it loop through all the rows. It's obvious that query function or get_array breaks the loop, but I don't know why.

query function:

    function query($query, $show_error=true)
    {
        $time_before = $this->get_real_time();

        if(!$this->db_id) $this->connect(DBUSER_D, DBPASS_D, DBNAME_D, DBHOST_D);

        if(!($this->query_id = mysqli_query($this->db_id, $query) )) {

            $this->mysql_error = mysqli_error($this->db_id);
            $this->mysql_error_num = mysqli_errno($this->db_id);

            if($show_error) {
                $this->display_error($this->mysql_error, $this->mysql_error_num, $query);
            }
    }

    $this->MySQL_time_taken += $this->get_real_time() - $time_before;    
        $this->query_num ++;

        return $this->query_id;
    }

get_array:

function get_array($query_id = '')
{
    if ($query_id == '') $query_id = $this->query_id;

    return mysqli_fetch_array($query_id);
}

Upvotes: 0

Views: 966

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

Can you try to instantiate another db object as

$db1 = new Your Class name();

while($row = $db->get_array()){
    ...
    $db1->query("DELETE FROM mytable WHERE id='{$row['id']}'");
}

Looks like when you are looping through $db and using the same object to delete the object is updated with the new data set and thus the loop stops.

Upvotes: 3

Related Questions