[英]Php array to column row in mysql table
I have array named olmali 我有一个名为olmali的数组
$olmali = $_POST['result'];
and print_r($olmali);
和
print_r($olmali);
Result is below : 结果如下:
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
)
Result are going to test column in SQL table in phpmyadmin like : 结果将在phpmyadmin中测试SQL表中的列,例如:
id test
1 array
But I expect : 但我期望:
id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on
How can I resolve this problem ? 我该如何解决这个问题? İs there any way and how can I do it.
有什么办法,我该怎么办。 PHP array to column row in MySQL table like that
PHP数组到MySQL表中的列行是这样的
You have to loop through the array and insert one by one. 您必须遍历数组并一个接一个地插入。
foreach($olmali as $v)
{
//insert query goes here
$sql = "INSERT INTO tbl_name (test) VALUES ('$v')";
// Then Execute the query
}
Another approach is to use Bulk Insert
, 另一种方法是使用
Bulk Insert
,
$sql = "INSERT INTO tbl_name (test) VALUES ";
foreach($olmali as $v)
{
//Concatenate values in bulk
$sql .= "('$v'),";
}
$sql = rtrim($sql, ','); // Remove extra comma at the end
// Then execute query
there are many ways 1:using Bulk insert 方法有很多1:使用批量插入
$qry = "INSERT INTO tableName(test) VALUES ";
foreach($olmali as $val){
$qry .="($val),";
}
$qry = preg_replace("/\,$/", ";", $qry);
$conn->query($sql);
2:using prepare statement preferred way 2:使用准备语句的首选方式
$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO tableName(test) VALUES(?)");
foreach($olmali as $val)
{
$stmt->bind_param('i',$val);
$stmt->execute();
}
$stmt->close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.