首页 > PHP foreach 抽风了?

PHP foreach 抽风了?

兄弟们,我现在正在更新一个表单里的一列数据。我选择了200行,然后用foreach逐个循环。但是发现不但更新了我选择的200行,连其余的3000行都更新了。希望帮忙看下!

$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;

提取的$record数量已通过print_r验证确实为200个。

谢谢!

更新:
在endforeach之后加上$this->db->last_query();试过了。返回结果是:UPDATE user SET weekday = 1。貌似没有包括和$record相关的条件。哪里出了问题呢?

更新:
以下兄弟们提供的回答多次试过都不行。大家可不可以帮忙想下有什么不用foreach的其他方法?


楼主这用的是CI么?单纯看这看循环应该是没有问题的。
这了个脚本测试也没有问题。

        $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 '<br \/>';
        endforeach;

输出
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'

楼主可能需要提供更多信息。


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 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.

好奇怪的语法,没见过。不过我觉得是foreach最后的update有问题。试试这样:

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

你的user_id不是uniq_key?
建议你跟到db这个对象里,把sql打印出来看一下,确认是否由于最终update的SQL条件出了问题


假如你的 user_id 是自增的,试试这个看看可以不。

$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);

你这样试试:

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

再不行的话,就别用替代语法了,直接写foreach


怀疑是$this->db的使用有问题,上面你有db做查询,下面又直接拿来update,总感觉不太对

【热门文章】
【热门文章】