Reputation: 1
i have .json file [categories.json]
like this
{
"apple": [
"fruit",
"15"
],
"cat": [
"animal",
"400"
],
"pumpkin": [
"vegetables",
"20"
],
"orange": [
"fruit",
"30"
]
}
i want to insert json object into mysql using loop php like this
|___id__|___ product__|_____type_____|__price__|
| 1 | apple | fruit | 15 |
| 2 | cat | animal | 400 |
| 3 | pumpkin | vegetables | 20 |
| 4 | orange | fruit | 30 |
how can i do thank you
Upvotes: 0
Views: 224
Reputation: 6121
$file = 'www.mysite.com/categories.json';
$data = json_decode(file_get_contents($file), true);
foreach($data as $product => $row){
$sql = "INSERT INTO product ";
$sql .= "SET product='".mysql_real_escape_string($product)."',type='".mysql_real_escape_string($row[0])."',price=".mysql_real_escape_string($row[1]);
mysql_query($sql);
} // hoping your id field in db is auto_increment
Upvotes: 2
Reputation: 1170
just use json_decode to convert your Json file into a an array and then loop and insert as you'd do that with an array
Upvotes: 1