开发者

Insert php nested array into mysql table

what is the optimal way for me to insert and array of 1000 lines and 10 columns each into a mysql table below is how i display it so it would be a similar construct but i need some directions

foreac开发者_开发知识库h ($stack as $val) {
    print "<tr>\n";
    foreach ($val as $no) {
       print " <td>$no</td>\n";}
 print "</tr>\n"; 
}


You can insert multiple rows with a single insert as follows :

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

look at implode() to create the values string from your array


Better way to insert thousands of data into DB is to use implode function implode


i'm guessing u got something like this

$stack = array("man1" => array("name"=>"ik", "class"=>"highskl", "age"=> "12"),"man1" => array("name"=>"ijk", "class"=>"higkl", "age"=> "13"));

and you want to insert them into a table, try and use the table fields as index for the inner arrays then adjust the code to look like this

    foreach ($stack as $entry => $value) {
$query = "INSERT INTO table set ";
foreach ($value as $key => $val) {
   $query .= $key ."= ".$val.",";}
 //use a string function to remove the last comma
 $result = mysql_query($query) or die("Error in Query ".$entry." ",mysql_error());
//this helps to track error..
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜