为什么在 SELECT 更改 2 行而不是 1 行后使用 UPDATE? [英] Why UPDATE used after SELECT chages 2 rows instead of one?

查看:27
本文介绍了为什么在 SELECT 更改 2 行而不是 1 行后使用 UPDATE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪,让我发疯,我有一个必须不时更新的制造商列表.我必须选择最旧的更新,然后将当前时间写为最后更新时间.

This is very weird and it is driving me crazy, I have a list of manufacturers that have to be updated from time to time. I have to select the oldest updated and then write the current time as the last update time.

问题是更新查询更新了两行,而不是一行.它使用正确的制造商 ID 更新该行以及下一个.

The problem is that the update query UPDATES TWO ROWS, instead of one. It updates the row with the correct manufacturer_id and also the next one.

另一件奇怪的事情是,如果我在两个查询之间放置 ECHO,一切都正常.

ANOTHER ODD THING IS THAT EVERYTHING WORKS NORMALLY IF I PUT AN ECHO BETWEEN THE TWO QUERIES.

我已经在 Mac XAMPP 服务器、Windows XAMPP 服务器和 Linux 服务器 (Hostgator) 上进行了测试.它仅在 Windows 服务器上按预期工作,在 Mac 和 Linux 上,更新查询影响 2 行而不是 1 行.

I have tested on a Mac XAMPP server, on a Windows XAMPP server and on a Linux server (Hostgator). It works as expected only on the Windows server, on the Mac and Linux the update query affects 2 rows instead of one.

请测试以下脚本:

制作一个文件 test.php 并粘贴以下代码:

make a file test.php and paste the following code:

$connection = mysql_connect('localhost', 'root', '') 
    or die("Unable to connect to MySQL");
$select = mysql_select_db('test',$connection) 
     or die("Could not select test");

$sql = '
SELECT manufacturer_id FROM
manufacturer
ORDER BY last_update LIMIT 1
';

$query = mysql_query($sql, $connection);
$manufacturer = mysql_fetch_assoc($query);

//echo $manufacturer['manufacturer_id'];

$sql = 'UPDATE manufacturer
SET last_update = '.time().'
WHERE manufacturer_id ='. $manufacturer['manufacturer_id'];
mysql_query($sql, $connection);

创建数据库测试并运行以下 SQL 查询以创建表 manufacturers:

create a database test and run the following SQL query to create the table manufacturers:

CREATE TABLE `manufacturer` (
  `manufacturer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `last_update` bigint(15) DEFAULT NULL,
  PRIMARY KEY (`manufacturer_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

LOCK TABLES `manufacturer` WRITE;
/*!40000 ALTER TABLE `manufacturer` DISABLE KEYS */;

INSERT INTO `manufacturer` (`manufacturer_id`, `last_update`)
VALUES
(1,0),
(2,0),
(3,0),
(4,0);

/*!40000 ALTER TABLE `manufacturer` ENABLE KEYS */;
UNLOCK TABLES;

调用 test.php,然后检查表以查看有多少行受到影响.如果只有一行受到影响,那么它会在您的服务器(可能是 Windows)上按预期工作.

Call test.php and then check the table to see how many rows have been affected. If only one row is affected then it works as expected on your server (probably Windows).

如果两行受到影响,那么它在您的服务器上也有相同的奇怪行为.现在取消选中 echo 行,您将看到它现在按预期工作.疯了!!!

If two rows are affected then it has the same strange behavior on your server too. Now uncheck the echo line and you'll see that it works as expected now. Crazy!!!

这是什么?好像有人在捉弄我.有人知道为什么会这样吗?

What is this? It's like somebody is playing tricks on me. Anybody has any idea why this is happening?

你认为这与某些 mysql 缓存或类似的东西有关吗???

Do you think this has something to do with some mysql cache or something like this???

非常感谢您的帮助!

推荐答案

感谢 Mark B(见评论)我发现浏览器是原因.更确切地说,Chrome 中的 firePHP 插件.禁用后,一切恢复正常.

Thanks to Mark B (see comments) I found out that the browser was the cause. More exactly firePHP plugin in Chrome. After I disabled it, everything returned to normal.

出于某种原因,firePHP 使浏览器在没有回显时调用页面两次.

For some reason, firePHP makes the browser to call the page twice when no echo is made.

这篇关于为什么在 SELECT 更改 2 行而不是 1 行后使用 UPDATE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆