[英]Array to column row with SQL Update column in MySQL table
I have array named olmali 我有一个名为olmali的数组
$olmali = $_POST['result'];
And 和
print_r($olmali);
Array ( [0] => 1 [1] => 1 [2] => 20 [3] => 2 [4] => 3 [5] => 5 [6] => 6 [7] => 7 [8] => 9 [9] => 8 [10] => 10 [11] => 11 [12] => 13 [13] => 12 [14] => 12 [15] => 14 [16] => 15 [17] => 16 [18] => 17 [19] => 17 [20] => 19 [21] => 20 )
I want to use SQL UPDATE command and I expect: 我想使用SQL UPDATE命令,我期望:
id test 1 1 2 1 3 20 4 2 5 3 6 ....and goes on
How can I resolve this problem? 我该如何解决这个问题? Is there any way and how can I do it.
有什么办法,我该怎么办。 PHP array to column row in MySQL table with UPDATE SQL command like that
PHP数组到MySQL表中的列行,使用类似的UPDATE SQL命令
Assuming 假设
$myArray = array (
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
);
then once you have a db connection using a PDO prepare binding and execute 然后使用PDO建立数据库连接后,准备绑定并执行
$stmt = $conn->prepare("Update my_tabe
set my_column = :my_value
where my_id_comn = :my_id +1" );
$stmt->bindParam(':my_value', $value);
$stmt->bindParam(':my_id', $key);
foreach ($myArray as $key => $value){
$stmt->execute();
}
I would suggest to start from the end with a for
loop and reassign every key with a value +1
. 我建议从
for
循环开始,然后重新分配每个值+1
键。 Then, you just have to remove the first index of the array with unset()
. 然后,您只需要使用
unset()
删除数组的第一个索引。 See the code below. 请参见下面的代码。
$olmali = [
0 => 1,
1 => 1,
2 => 20,
3 => 2,
4 => 3,
5 => 5,
6 => 6,
7 => 7,
8 => 9,
9 => 8,
10 => 10,
11 => 11,
12 => 13,
13 => 12,
14 => 12,
15 => 14,
16 => 15,
17 => 16,
18 => 17,
19 => 17,
20 => 19,
21 => 20
];
for($i = count($olmali); $i > 0; $i--) {
$olmali[$i] = $olmali[$i - 1];
}
unset($olmali[0]);
print_r($olmali);
Output: 输出:
Array ( [1] => 1 [2] => 1 [3] => 20 [4] => 2 [5] => 3 [6] => 5 [7] => 6 [8] => 7 [9] => 9 [10] => 8 [11] => 10 [12] => 11 [13] => 13 [14] => 12 [15] => 12 [16] => 14 [17] => 15 [18] => 16 [19] => 17 [20] => 17 [21] => 19 [22] => 20 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.