我正在尝试将数组插入mysql。数组的结构是
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
)
)
我创建表格的代码是
// 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)";
(上面的数组包含2个用户,但可以超过2k个用户)
如何将数组中的所有用户插入myusers表,并为每个用户分别提供信息?
答案 0 :(得分:0)
对foreach
数组值使用INSERT
。
$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",...);
}