Table of Contents
回复内容:
Home Backend Development PHP Tutorial mysql - PHP foreach 抽风了?

mysql - PHP foreach 抽风了?

Jun 06, 2016 pm 08:45 PM
codeigniter foreach mysql php

兄弟们,我现在正在更新一个表单里的一列数据。我选择了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>
Copy after login
Copy after login

提取的$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>
Copy after login
Copy after login

提取的$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>
Copy after login

你的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>
Copy after login

再不行的话,就别用替代语法了,直接写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);
Copy after login

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

<code>$this->db->update('user', $data)->where('user_id', $row->user_id);
</code>
Copy after login

楼主这用的是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;
Copy after login

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

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Useful Resources CakePHP Useful Resources Sep 10, 2024 pm 05:27 PM

The following resources contain additional information on CakePHP. Please use them to get more in-depth knowledge on this.

See all articles