I have two arrays like A & B.
我有两个数组,比如A和B。
A=[1,2,3,4] , B=[10,20,30,40]
I want to execute a mysql update query in a way like this.
我想以这样的方式执行一个mysql更新查询。
$abc1=mysql_query("update table set corr='1' WHERE id=10");
$abc1=mysql_query("update table set corr='2' WHERE id=20");
$abc1=mysql_query("update table set corr='3' WHERE id=30");
$abc1=mysql_query("update table set corr='4' WHERE id=40");
all these query execution in one go.
所有这些查询执行都是一次性完成的。
4 个解决方案
#1
1
Just loop em and use the index for the second array
只需循环em并使用第二个数组的索引
$as=[1,2,3,4] , $bs=[10,20,30,40];
foreach ($as as $key=>$val) {
$abc1=mysql_query("update table set corr='".$val."' WHERE id=".$bs[$key]);
}
Note: You shouldn't use mysql
use mysqli
instead
注意:不应该使用mysqluse mysqliinstead
Note: Always escape
注:总是逃避
#2
1
Using array_combine()
, you can create a new array, specify one array as the keys, the other as values in the new array. Then it's just a matter of looping the resulting array, and execute queries. Since you now have one array, use a foreach
loop and use the keys (in this case, all the values from $a
) and values (in this case, all the values from $b
) as the values you're setting.
使用array_combine(),可以创建一个新数组,将一个数组指定为键,另一个指定为新数组中的值。然后,只需对结果数组进行循环,并执行查询。由于您现在有一个数组,使用foreach循环并使用键(在本例中为$a的所有值)和值(在本例中为$b的所有值)作为您设置的值。
This assumes that the amount of entries in both array is always the same. If they are not of the same size, array_combine()
will return false - you can use that as a check before performing the queries.
这假定两个数组中的条目数量总是相同的。如果它们的大小不同,array_combine()将返回false——您可以在执行查询之前使用它作为检查。
$a = [1, 2, 3, 4];
$b = [10, 20, 30, 40];
$result = array_combine($a, $b);
foreach ($result as $k=>$v) {
mysql_query("UPDATE table SET corr='$k' WHERE id = '$v'");
}
That being said, this query is vulnerable to SQL injection, and you should upgrade to a newer API which supports parameterized queries with placeholders (mysqli_*
or PDO). The mysql_*
API was deprecated in PHP 5.6 and removed entirely in PHP7.
也就是说,这个查询容易受到SQL注入的影响,您应该升级到一个新的API,该API支持带占位符的参数化查询(mysqli_*或PDO)。在PHP 5.6中不支持mysql_* API,在PHP7中完全删除。
- Why shouldn't I use mysql_* functions in PHP?
- 为什么我不应该在PHP中使用mysql_*函数呢?
- How can I prevent SQL injection in PHP?
- 如何防止PHP中的SQL注入?
- Manual for
array_combine()
- 手册合二为一()
#3
0
Assuming both arrays length are same,
假设两个数组长度相同,
<?php
$A=[1,2,3,4];
$B=[10,20,30,40];
for($i=0;$i<sizeof($A);$i++){
mysql_query("update table set corr='".$A[$i]."' WHERE id='".$B[$i]."'");
}
?>
#4
0
$a=[1,2,3,4];$b=[10,20,30,40];
$res=array_combine($a, $b );
foreach ($res as $key => $value) {
$abc1=mysql_query("update table set corr='".$key."' WHERE id=".$value);
}
#1
1
Just loop em and use the index for the second array
只需循环em并使用第二个数组的索引
$as=[1,2,3,4] , $bs=[10,20,30,40];
foreach ($as as $key=>$val) {
$abc1=mysql_query("update table set corr='".$val."' WHERE id=".$bs[$key]);
}
Note: You shouldn't use mysql
use mysqli
instead
注意:不应该使用mysqluse mysqliinstead
Note: Always escape
注:总是逃避
#2
1
Using array_combine()
, you can create a new array, specify one array as the keys, the other as values in the new array. Then it's just a matter of looping the resulting array, and execute queries. Since you now have one array, use a foreach
loop and use the keys (in this case, all the values from $a
) and values (in this case, all the values from $b
) as the values you're setting.
使用array_combine(),可以创建一个新数组,将一个数组指定为键,另一个指定为新数组中的值。然后,只需对结果数组进行循环,并执行查询。由于您现在有一个数组,使用foreach循环并使用键(在本例中为$a的所有值)和值(在本例中为$b的所有值)作为您设置的值。
This assumes that the amount of entries in both array is always the same. If they are not of the same size, array_combine()
will return false - you can use that as a check before performing the queries.
这假定两个数组中的条目数量总是相同的。如果它们的大小不同,array_combine()将返回false——您可以在执行查询之前使用它作为检查。
$a = [1, 2, 3, 4];
$b = [10, 20, 30, 40];
$result = array_combine($a, $b);
foreach ($result as $k=>$v) {
mysql_query("UPDATE table SET corr='$k' WHERE id = '$v'");
}
That being said, this query is vulnerable to SQL injection, and you should upgrade to a newer API which supports parameterized queries with placeholders (mysqli_*
or PDO). The mysql_*
API was deprecated in PHP 5.6 and removed entirely in PHP7.
也就是说,这个查询容易受到SQL注入的影响,您应该升级到一个新的API,该API支持带占位符的参数化查询(mysqli_*或PDO)。在PHP 5.6中不支持mysql_* API,在PHP7中完全删除。
- Why shouldn't I use mysql_* functions in PHP?
- 为什么我不应该在PHP中使用mysql_*函数呢?
- How can I prevent SQL injection in PHP?
- 如何防止PHP中的SQL注入?
- Manual for
array_combine()
- 手册合二为一()
#3
0
Assuming both arrays length are same,
假设两个数组长度相同,
<?php
$A=[1,2,3,4];
$B=[10,20,30,40];
for($i=0;$i<sizeof($A);$i++){
mysql_query("update table set corr='".$A[$i]."' WHERE id='".$B[$i]."'");
}
?>
#4
0
$a=[1,2,3,4];$b=[10,20,30,40];
$res=array_combine($a, $b );
foreach ($res as $key => $value) {
$abc1=mysql_query("update table set corr='".$key."' WHERE id=".$value);
}