Php 将多维数组插入mysql
我想使用insert将多维数组插入mysql 使用以下代码插入,如下所示Php 将多维数组插入mysql,php,Php,我想使用insert将多维数组插入mysql 使用以下代码插入,如下所示 "insert into table (col1) value(1)"; "insert into table (col2) value(2)"; "insert into table (col3) value(3)"; "insert into table (col4) value(6)"; ... "insert into table (col1) value(7)"; "insert into table (col
"insert into table (col1) value(1)";
"insert into table (col2) value(2)";
"insert into table (col3) value(3)";
"insert into table (col4) value(6)";
...
"insert into table (col1) value(7)";
"insert into table (col2) value(5)";
"insert into table (col2) value(3)";
"insert into table (col2) value(8)";
...
相反,它应该插入
"insert into table (col1,col2,col3,col4,col5) values(1,2,3,6,7) "
"insert into table (col1,col2,col3,col4,col5) values(7,5,3,8) "
...
$res_arr= array(
0=>array(1,2,3,6),
1=>array(7,5,3,8),
2=>array(6,5,9,8),
....
10000
);
下面是我使用的代码
$maketbl = "CREATE TABLE $table(";
//$maketemp .= " id int(11) , ";
for($i=0;$i<= 5;$i++)
{
$maketbl .= " COLUMN_$i varchar(128), ";
//echo "<br>";
}
$maketbl .=")";
$maketbl = substr($maketbl,0,-3);
$maketbl.=" )";
mysql_query( $maketbl ) or die ( "Sql error : " . mysql_error ( ) );
foreach ($res_arr as $k=>$subArray)
{
foreach ($subArray as $id=>$value)
{
@$avArray[$id] = $value;
if(!empty($avArray[$id]))
{
mysql_query("insert into $table(COLUMN_$id) values($avArray[$id]))";
}
}
}
$maketbl=“创建表$TABLE(”;
//$maketemp.=“id int(11)”;
对于($i=0;$i$子阵列)
{
foreach($id=>$value的子数组)
{
@$avArray[$id]=$value;
如果(!empty($avArray[$id]))
{
mysql_查询(“插入到$table(COLUMN_$id)值($avArray[$id])”;
}
}
}
如果愿意,可以在每个语句之间添加新行
输出:
insert into test_table (a,b,c,d) values ('1','2','3','6'); insert into test_table (a,b,c,d) values ('7','5','3','8'); insert into test_table (a,b,c,d) values ('6','5','9','8');
计数5是动态的,它可以包含16列
values(1,2,3,6,7); "
insert into test_table (a,b,c,d) values ('1','2','3','6'); insert into test_table (a,b,c,d) values ('7','5','3','8'); insert into test_table (a,b,c,d) values ('6','5','9','8');