PHP->sql UPDATE 行,其中来自同一个表中的其他 2 行的数据合并在一起
桌子=位置 字段 = 地址、街道、郊区
地址字段最初为空,街道和郊区已填充。我尝试使用街道和郊区数据更新地址字段,但它不起作用。只允许我更新一个字段。使用下面的代码,地址字段将更新为 0...但是,如果我从更新中删除其中一个变量,那么它将仅使用街道或郊区正确更新。
我这样做的原因是因为向我提供数据的方式是这种格式,并且我手动执行此操作需要很长时间。
while ($row=$result->fetch_assoc()) {
$street=$row['street'];
$suburb=$row['suburb'];
$mysqli->query("UPDATE address SET address = $street+$suburb");
echo($street);
}
}
Table = location
Fields = address, street, suburb
address fields are initially empty, street and suburb are populated. I have tried to update the address field with the street and suburb data but its not working. only allows me to update with one field. With the code below the address field would be updated with 0's... But if i remove one of the variables from the update then it will update correctly with just either the street or suburb.
The reason why im doing this is because the way the data is given to me is in that format and would take too long for me to manually do it.
while ($row=$result->fetch_assoc()) {
$street=$row['street'];
$suburb=$row['suburb'];
$mysqli->query("UPDATE address SET address = $street+$suburb");
echo($street);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要忘记在 UPDATE 查询中放置 WHERE 子句,除非您希望整个表具有相同的地址列。试试这个
Don't forget to put a WHERE clause in your UPDATE query, unless you want your whole table to have the same adress column. Try this
Try
+
不是 php 中的连接运算符。因此,请尝试使用.
另外,如果您没有
WHERE
子句,那么所有行都会更新!Try
+
is not a concatenation operator in php. So try using.
Also, if you dont have the
WHERE
clause, then all the rows will get updated!!!尝试使用:
Try with:
您不必将结果获取到 PHP,然后对每一行运行
UPDATE
。您现在似乎正在向数据库发送一个SELECT
和大量(数千或数百万,取决于表的大小)UPDATE
语句。您可以使用一个
UPDATE
语句更新整个表(所有行!):如果(稍后)您的某些行已经在
address 列,您只能更新地址为空的行:
您还应该检查
address
字段的大小是否大于或至少等于CHAR_LENGTH(street) + CHAR_LENGTH(郊区)+ 1
You don't have to fetch results to PHP and then run an
UPDATE
for every row. It seems you are now sending oneSELECT
and numerous (thousands or millions, depends on the size of your table) ofUPDATE
statements to the database.You can update the whole table (all rows!) with one
UPDATE
statement:If (later on) some of your rows have already data in the
address
column, you can update only rows with empty address:You should also check that the size of
address
field is bigger or at least equal to the maximum ofCHAR_LENGTH(street) + CHAR_LENGTH(suburb) + 1