簡單,只是轉換JSON對象數組,然後走線槽,以構建INSERT查詢:
SQL:
CREATE TABLE `gallery` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`lowsrc` varchar(255) DEFAULT NULL,
`fullsrc` varchar(255) DEFAULT NULL,
`description` text,
`category` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
PHP:
<?php
$json = '
[{
"lowsrc": "thumbnails\/1.jpg",
"fullsrc": "full\/1.jpg",
"description": "Zweifellos Mondbetont",
"category": "photography"
}, {
"lowsrc": "thumbnails\/2.jpg",
"fullsrc": "full\/2.jpg",
"description": "Mehmet Dere",
"category": "drawing"
}, {
"lowsrc": "thumbnails\/3.jpg",
"fullsrc": "full\/3.jpg",
"description": "Samantha Keely Smith",
"category": "drawing"
}, {
"lowsrc": "thumbnails\/4.jpg",
"fullsrc": "full\/4.jpg",
"description": "Kumi Yamashita. Created with thread and nails.",
"category": "handmade"
}, {
"lowsrc": "thumbnails\/5.jpg",
"fullsrc": "full\/5.jpg",
"description": "Alexander Semenov. Underwater macro photography.",
"category": "photography"
}, {
"lowsrc": "thumbnails\/6.jpg",
"fullsrc": "full\/6.jpg",
"description": "Borondo. Street Art.",
"category": "street art"
}, {
"lowsrc": "thumbnails\/7.jpg",
"fullsrc": "full\/7.jpg",
"description": "Anka Zhuravleva",
"category": "photography"
}, {
"lowsrc": "thumbnails\/8.jpg",
"fullsrc": "full\/8.jpg",
"description": "Guy Denning. Sketches.",
"category": "drawing"
}]';
$data = json_decode($json,true);
$link = mysqli_connect('dbhost', 'user', 'password', 'dbname') or die(mysqli_error($link));
foreach($data as $key=>$arr){
$fields = array();
$values = array();
foreach($arr as $k=>$v){
$fields[] = $k;
$values[] = mysqli_real_escape_string($link, $v);
}
$query = "INSERT INTO gallery (".implode(', ',$fields).") VALUES ('".implode("', '",$values)."')";
mysqli_query($link, $query) or die(mysqli_error($link));
}
?>
SQL生成輸出:
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/1.jpg', 'full/1.jpg', 'Zweifellos Mondbetont', 'photography');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/2.jpg', 'full/2.jpg', 'Mehmet Dere', 'drawing');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/3.jpg', 'full/3.jpg', 'Samantha Keely Smith', 'drawing');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/4.jpg', 'full/4.jpg', 'Kumi Yamashita. Created with thread and nails.', 'handmade');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/5.jpg', 'full/5.jpg', 'Alexander Semenov. Underwater macro photography.', 'photography');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/6.jpg', 'full/6.jpg', 'Borondo. Street Art.', 'street art');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/7.jpg', 'full/7.jpg', 'Anka Zhuravleva', 'photography');
INSERT INTO gallery (lowsrc, fullsrc, description, category) VALUES ('thumbnails/8.jpg', 'full/8.jpg', 'Guy Denning. Sketches.', 'drawing');
數據庫內容:
mysql> select * from gallery;
+----+------------------+------------+--------------------------------------------------+-------------+
| id | lowsrc | fullsrc | description | category |
+----+------------------+------------+--------------------------------------------------+-------------+
| 1 | thumbnails/1.jpg | full/1.jpg | Zweifellos Mondbetont | photography |
| 2 | thumbnails/2.jpg | full/2.jpg | Mehmet Dere | drawing |
| 3 | thumbnails/3.jpg | full/3.jpg | Samantha Keely Smith | drawing |
| 4 | thumbnails/4.jpg | full/4.jpg | Kumi Yamashita. Created with thread and nails. | handmade |
| 5 | thumbnails/5.jpg | full/5.jpg | Alexander Semenov. Underwater macro photography. | photography |
| 6 | thumbnails/6.jpg | full/6.jpg | Borondo. Street Art. | street art |
| 7 | thumbnails/7.jpg | full/7.jpg | Anka Zhuravleva | photography |
| 8 | thumbnails/8.jpg | full/8.jpg | Guy Denning. Sketches. | drawing |
+----+------------------+------------+--------------------------------------------------+-------------+
8 rows in set
這是一個我nedeed!謝謝 – Natalino