I am trying to insert an array into mysql. The structure of the array is
Array
(
[0] => Array
(
[.id] => *1
[name] => vpn
[service] => pptp
[caller-id] =>
[password] => vpn
[profile] => VPN
[routes] =>
[limit-bytes-in] => 0
[limit-bytes-out] => 0
[last-logged-out] => mar/12/2016 22:20:26
[disabled] => false
)
[1] => Array
(
[.id] => *2
[name] => 123
[service] => pppoe
[caller-id] =>
[password] => 123
[profile] => VPN
[routes] =>
[limit-bytes-in] => 0
[limit-bytes-out] => 0
[last-logged-out] => apr/04/2016 15:46:41
[disabled] => false
)
)
The code I've created my table with is
// sql to create table
$sql = "CREATE TABLE myusers (
id INT(1) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Number VARCHAR(255) NOT NULL,
Name VARCHAR(255) NOT NULL,
Service VARCHAR(255) NOT NULL,
Caller_id VARCHAR(255) NOT NULL,
Password VARCHAR(255) NOT NULL,
Profile VARCHAR(255) NOT NULL,
Limit_bytes_in VARCHAR(255) NOT NULL,
Limit_bytes_out VARCHAR(255) NOT NULL,
Last_logged_out VARCHAR(255) NOT NULL,
Disabled VARCHAR(255) NOT NULL,
reg_date TIMESTAMP)";
(The array above contain 2 users but it can be more than 2k users)
How can I insert all users from the array to myusers table with separated info for each user?
Use foreach
for INSERT
the array values.
$sample_array = Array(Array(
".id" => "1","name" => "vpn" ,"service" => "pptp",
"caller-id" => "",
"password" => "vpn",
"profile" => "VPN",
"routes" => "",
"limit-bytes-in" => "0",
"limit-bytes-out" => "0",
"last-logged-out" => "mar/12/2016 22:20:26",
"disabled" => false
),
Array
(
".id" => "2",
"name" => "123",
"service" => "pppoe",
"caller-id" => "",
"password" => "123",
"profile" => "VPN",
"routes" => "",
"limit-bytes-in" => "0",
"limit-bytes-out" => "0",
"last-logged-out" => "apr/04/2016 15:46:41",
"disabled" => false
));
foreach($sample_array as $sample_value)
{
//Make the INSERT Query
INSERT INTO table_name(column_name1,column_name2,...) VALUES ("value1","value2",...);
}