Why is mysql-PHPforeach exhausted?

Source: Internet
Author: User
Guys, I am updating a column of data in a form. I select 200 rows and use foreach to loop one by one. However, I found that not only the selected 200 rows were updated, but the remaining 3000 rows were updated. Hope you can check it out! The number of $ Records extracted by {code...} has been verified by print_r... and I am updating a column of data in a form. I select 200 rows and use foreach to loop one by one. However, I found that not only the selected 200 rows were updated, but the remaining 3000 rows were updated. Hope you can check it out!

$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 $ records has been verified by print_r to be 200.

Thank you!

Update:
After endforeach, add $ this-> db-> last_query (); tried. The returned result is: UPDATE user SET weekday = 1. It seems that no conditions related to $ record are included. What's wrong?

Update:
The answers provided by the following brothers cannot be answered after multiple attempts. Can you help me think about other methods without foreach?

Reply content:

Guys, I am updating a column of data in a form. I select 200 rows and use foreach to loop one by one. However, I found that not only the selected 200 rows were updated, but the remaining 3000 rows were updated. Hope you can check it out!

$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 $ records has been verified by print_r to be 200.

Thank you!

Update:
After endforeach, add $ this-> db-> last_query (); tried. The returned result is: UPDATE user SET weekday = 1. It seems that no conditions related to $ record are included. What's wrong?

Update:
The answers provided by the following brothers cannot be answered after multiple attempts. Can you help me think about other methods without foreach?

Problem solved. you cannot limit what the update function does. it will do the update for all rows in the table despite the foreach function. in order to accomplish the same objective, I 've resorted:

        $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?
We recommend that you print the SQL statement in the db object and check whether the SQL condition of the final update statement is incorrect.

I suspect that there is a problem with the use of $ this-> db. You have a db for the query above, and I will use the update directly below. I don't feel quite right.

Try it like this:

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

If it doesn't work, don't replace the syntax and write foreach directly.

If your user_id is auto-incrementing, 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);

Strange syntax. I have never seen it. However, I think there is a problem with foreach's last update. Try this:

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

Is CI used by the landlord? Simply looking at this, there should be no problem with the loop.
There is no problem with this script test.

$ Record = $ this-> db-> get ('user', 10); $ data = array ('resetpwd' => 5 ,); // It is better to put the foreach ($ record-> result () as $ rows) outside the loop: $ this-> db-> where ('userid ', $ rows-> userid); $ this-> db-> update ('user', $ data); // print_r ($ rows ); print_r ($ this-> db-> last_query (); echo'
'; Endforeach;

Output
UPDATEm_userSETresetpwd= 5 WHEREuserid= '1'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '2'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '3'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '4'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '5'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '6'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '7'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '8'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '9'
UPDATEm_userSETresetpwd= 5 WHEREuserid= '10'

The owner may need to provide more information.

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.