0

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

foreach ($stack as $val) {
    print "<tr>\n";
    foreach ($val as $no) {
       print " <td>$no</td>\n";}
 print "</tr>\n"; 
}

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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..
}

1 Comment

sorry this came late...power failure!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.