兄弟们,我现在正在更新一个表单里的一列数据。我选择了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个。
立即学习“PHP免费学习笔记(深入)”;
谢谢!
更新:
在endforeach之后加上$this->db->last_query();试过了。返回结果是:UPDATE user SET weekday = 1。貌似没有包括和$record相关的条件。哪里出了问题呢?
更新:
以下兄弟们提供的回答多次试过都不行。大家可不可以帮忙想下有什么不用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个。
立即学习“PHP免费学习笔记(深入)”;
谢谢!
更新:
在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:
MediPro乡镇政府门户网站系统,适合乡镇政府机构创建地方门户网站,用以宣传本地资源,实现政务公开,促进乡镇基层信息化建设。本系统基于PHP+MYSQL开发,预设了乡镇风采、党政机构、政务公开、投资指南、服务导航、文件下载、公众互动、领导信箱等乡镇政府门户网站常用的栏目和测试数据,采用适合乡镇政府门户网站的专用模版,增强了系统的针对性和易用性。除了文章系统、图文系统、下载系统、社区交流、反馈表单
0
$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.
你的user_id不是uniq_key?
建议你跟到db这个对象里,把sql打印出来看一下,确认是否由于最终update的SQL条件出了问题
怀疑是$this->db的使用有问题,上面你有db做查询,下面又直接拿来update,总感觉不太对
你这样试试:
$this->db->update('user',$data,array('user_id'=>$row->user_id));
再不行的话,就别用替代语法了,直接写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有问题。试试这样:
$this->db->update('user', $data)->where('user_id', $row->user_id);
楼主这用的是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'
楼主可能需要提供更多信息。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号