mysql-php foreach jerking off?

Source: Internet
Author: User
Tags php foreach
Brothers, I am now updating a list of data in a form. I chose 200 rows, and then I loop through it with foreach. But the discovery not only updated the 200 rows I chose, but even the remaining 3000 lines were updated. Hope to help look!

$this->db->where('weekday', 5);$this->db->where('source', 'site');$record = $this->db->get('user', 200);print_r($record->num_rows());foreach ($record->result() as $row) :$data = array(    'weekday' => 1,);$this->db->where('user_id', $row->user_id); $this->db->update('user', $data);endforeach;

The number of extracted $record has been verified by Print_r to be 200 indeed.

Thank you!

Update:
After Endforeach, add $this->db->last_query () and try it. The return result is: UPDATE user SET weekday = 1. Does not seem to include conditions related to $record. Where's the problem?

Update:
The answers given by the following brothers have been tried many times. Can we help you think of any other ways without foreach?

Reply content:

Brothers, I am now updating a list of data in a form. I chose 200 rows, and then I loop through it with foreach. But the discovery not only updated the 200 rows I chose, but even the remaining 3000 lines were updated. Hope to help look!

$this->db->where('weekday', 5);$this->db->where('source', 'site');$record = $this->db->get('user', 200);print_r($record->num_rows());foreach ($record->result() as $row) :$data = array(    'weekday' => 1,);$this->db->where('user_id', $row->user_id); $this->db->update('user', $data);endforeach;

The number of extracted $record has been verified by Print_r to be 200 indeed.

Thank you!

Update:
After Endforeach, add $this->db->last_query () and try it. The return result is: UPDATE user SET weekday = 1. Does not seem to include conditions related to $record. Where's the problem?

Update:
The answers given by the following brothers have been tried many times. Can we help you think of any other ways without foreach?

Problem solved. You cannot limit what the update function does. The It would do the update for all rows in the table despite the foreach function. In order to accomplish the same objective, I ' ve resorted to:

        $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200";        $this->db->query($update);         $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200";        $this->db->query($update);         $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200";        $this->db->query($update);         $update = "UPDATE user SET weekday = 2 WHERE user_category=4 LIMIT 200";        $this->db->query($update);                 ...... all the way to the end. Of course I used a while loop to do the above.

Your user_id is not uniq_key?
It is recommended that you follow the DB object, print out the SQL and check to see if there is a problem with the SQL condition of the final update.

Suspect is the use of $this->db problem, above you have db to do query, below and directly to update, always feel not quite right

You try this:

$this->db->update('user',$data,array('user_id'=>$row->user_id));

If you don't, don't use alternative syntax, write foreach directly.

If your user_id is self-increasing, try this to see if you can.

$query = $this->db->get_where('user', array('weekday' => 5, 'source' => 'site'), 200);$result = $query->result();$lastRecord = array_pop($result);$data = array(    'weekday' => 1,);$this->db->update('user', $data)->where('weekday', 5)->where('source', 'site')->where('user_id > ', $lastRecord->user_id - 1);

It's a strange grammar, never seen. But I think it's a problem with the last update of foreach. Try this:

$this->db->update('user', $data)->where('user_id', $row->user_id);

Landlord this use is CI? Simply looking at this cycle should be no problem.
There's no problem with a script test.

        $record  = $this->db->get('user',10);        $data = array(            'resetpwd'  => 5,        );      //这个最好还是放在循环外面        foreach ($record->result() as $rows):            $this->db->where('userid',$rows->userid);            $this->db->update('user',$data);            //print_r($rows);            print_r($this->db->last_query());            echo '
'; endforeach;

Output
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 1 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 2 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 3 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 4 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 5 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 6 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 7 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 8 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 9 '
UPDATE m_user SET resetpwd = 5 WHERE userid = ' 10 '

Landlord may need to provide more information.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.