Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将多维数组插入mysql_Php - Fatal编程技术网

Php 将多维数组插入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将多维数组插入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 (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');