Insert a php array into mysql table


Following code shows that how can insert php array into mysql table directly. Sometime we have big array data and need to insert in mysql table, in that case following code will helpful for you.

$array = array(
"a" => array("100","test1"),
"b" => array("200","test2"),
"c" => array("300","test3"),
);

foreach ($array as $key => $value) {
$a = $array[$key][0];
$b = $array[$key][1];
$query ="INSERT INTO tablename (key, field1, field2) VALUES ( '".$key."','". $a."','".$b."' )";
mysql_query($query);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s