mysql - PHP foreach 抽风了?
兄弟们,我现在正在更新一个表单里的一列数据。我选择了200行,然后用foreach逐个循环。但是发现不但更新了我选择的200行,连其余的3000行都更新了。希望帮忙看下!
<code>$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; </code>
提取的$record数量已通过print_r验证确实为200个。
谢谢!
更新:
在endforeach之后加上$this->db->last_query();试过了。返回结果是:UPDATE user SET weekday = 1。貌似没有包括和$record相关的条件。哪里出了问题呢?
更新:
以下兄弟们提供的回答多次试过都不行。大家可不可以帮忙想下有什么不用foreach的其他方法?
回复内容:
兄弟们,我现在正在更新一个表单里的一列数据。我选择了200行,然后用foreach逐个循环。但是发现不但更新了我选择的200行,连其余的3000行都更新了。希望帮忙看下!
<code>$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; </code>
提取的$record数量已通过print_r验证确实为200个。
谢谢!
更新:
在endforeach之后加上$this->db->last_query();试过了。返回结果是:UPDATE user SET weekday = 1。貌似没有包括和$record相关的条件。哪里出了问题呢?
更新:
以下兄弟们提供的回答多次试过都不行。大家可不可以帮忙想下有什么不用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 to:
<code> $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. </code>
你的user_id不是uniq_key?
建议你跟到db这个对象里,把sql打印出来看一下,确认是否由于最终update的SQL条件出了问题
怀疑是$this->db的使用有问题,上面你有db做查询,下面又直接拿来update,总感觉不太对
你这样试试:
<code>$this->db->update('user',$data,array('user_id'=>$row->user_id)); </code>
再不行的话,就别用替代语法了,直接写foreach
假如你的 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);
好奇怪的语法,没见过。不过我觉得是foreach最后的update有问题。试试这样:
<code>$this->db->update('user', $data)->where('user_id', $row->user_id); </code>
楼主这用的是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'
楼主可能需要提供更多信息。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

如何修復 MySQL 8.4 上的 mysql_native_password 未載入錯誤

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發
