我需要更新数据库中的 40 行,从 userID
处开始。是一场比赛。我不想确定行号,因为最终这个数据库会很大。
我想做的只是简单地说:
"Update these the next 40 rows with my array where userID = myUserID"
这是我所拥有的:
<?php
// connect to the database and select the correct database
$con2 = mysql_connect("localhost","Database","Password");
if (!$con2)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ifaves_code", $con2);
$i = 0;
while ($i < 40) {
//cycle through the array
$cycleThrough2 = $updatedUserNames[$i];
//$query = "UPDATE tblUserLinks SET `URLName` = '$cycleThrough2' WHERE userID = '" . $mainUID . "' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
++$i;
}
mysql_close();
?>
变量
$mainUID
设置正确,我遇到的问题是数据库中似乎没有更新。如何更改我现有的代码以接收我想要的行为?
最佳答案
我不认为这是因为您将查询构建语句注释掉了...
//$query = "UPDATE tblUserLinks SET `URLName` = '$cycleThrough2' WHERE userID = '" . $mainUID . "' LIMIT 1";
如果这只是调试的结果并且在此之前无法正常工作,则必须调用
mysql_error()
紧随其后 mysql_query()
看看为什么它失败了。$result = mysql_query($query);
if (!$result) echo mysql_error();
另外,您正在使用
LIMIT 1
最后,但userID
WHERE 子句永远不会改变。因此,您在每个循环中重复更新同一行 40 多次。您需要的是在 WHERE 子句中识别已修改的行并排除它们的方法。否则,同一行(第一个匹配项)将始终被 LIMIT 1
捕获。并更新。
关于php - SQL 多重更新语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7057013/