为什么在SELECT之后使用UPDATE更改2行而不是1行


Why UPDATE used after SELECT chages 2 rows instead of one?

这是非常奇怪的,它把我逼疯了,我有一个制造商的名单,必须不时更新。我必须选择最老的更新,然后将当前时间作为最后一次更新时间。

问题是更新查询更新了两行,而不是一行。它使用正确的manufacturer_id和下一个更新行。

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

我在Mac XAMPP服务器、Windows XAMPP服务器和Linux服务器(Hostgator)上进行了测试。它只在Windows服务器上工作,在Mac和Linux上,更新查询影响2行而不是1行。

请测试以下脚本:

创建一个test.php文件并粘贴以下代码:

$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 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)上按预期工作。

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

这是什么?就像有人在捉弄我一样。有人知道为什么会这样吗?

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

谢谢你的帮助!

感谢Mark B(见注释),我发现浏览器是原因。更确切地说,firePHP插件在Chrome。在我关闭它之后,一切都恢复正常了。

由于某些原因,firePHP使浏览器在没有回显时调用页面两次。