JSON到MYSQL - 是JSON格式的响应正确 - 循环是否正常? [英] JSON to MYSQL - is JSON response formatted correctly - looping properly?

查看:148
本文介绍了JSON到MYSQL - 是JSON格式的响应正确 - 循环是否正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确定 - 我试图为以将其插入到所谓的食谱一个MySQL表如下加载一个JSON响应(从外部文件名为recipes.json其中包括数百食谱),这是格式化的:

OK - I am attempting to load a JSON response (from an external file called recipes.json which includes hundreds of recipes) which is formatted as follows in order to insert it into a MySQL table called "recipes":

{ "recipeName": "After Glow Smoothie",  "ingredients": "4 oz. (1/2 cup) pomegranate juice", "ingredients2": "4 oz. (1/2 cup orange juice)", "ingredients3": "2 scoops Vi-Shape shake mix", "ingredients4": "1 cup frozen pineapple", "ingredients5": "5 ice cubes"},
{ "recipeName": "All Berry Delight",     "ingredients": "8 oz. skim milk", "ingredients2": "2 scoops Vi-Shape shake mix", "ingredients3": "1/4 cup frozen raspberries", "ingredients4": "1/4 cup frozen blackberries", "ingredients5": "1/4 cup frozen strawberries", "ingredients6": "1/4 cup frozen dark cherries", "ingredients7": "5 ice cubes"}

我不使用数组太顺手,所以我很奇怪,为什么我不能通过食谱能循环,正确插入。是我的JSON格式不正确或我是这样的,我在我的主要code犯错误一个PHP小白。仅供参考,如下所示:

I am not too handy with arrays so I am wondering why I am not able loop through the recipes and insert them properly. Is my JSON malformed or am I such a PHP noob that I am making mistakes in my main code. For reference it as follows:

<?php
header('Content-Type: text/html; charset=utf-8');

$hostname_ndb = "localhost";
$database_ndb = "test";
$username_ndb = "root";
$password_ndb = "root";
$ndb = mysql_pconnect($hostname_ndb, $username_ndb, $password_ndb) or trigger_error(mysql_error(),E_USER_ERROR); 

$url = "http://localhost:8888/shakerecipes/recipes.json";


$json = file_get_contents($url);
// var_dump(json_decode($json, true));
$out = json_decode($json, true);


foreach($out["recipeName"] as $recipeNames) { 
$name = addslashes($recipeNames[recipeName]); 
$ingredients= addslashes($recipeNames[ingredients]); 
$ingredients2 = addslashes($recipeNames[ingredients2]);
$ingredients3 = addslashes($recipeNames[ingredients3]);
$ingredients4 = addslashes($recipeNames[ingredients4]);
$ingredients5 = addslashes($recipeNames[ingredients5]);
$ingredients6 = addslashes($recipeNames[ingredients6]);
$ingredients7 = addslashes($recipeNames[ingredients7]);
$ingredients8 = addslashes($recipeNames[ingredients8]);
$ingredients9 = addslashes($recipeNames[ingredients9]);

mysql_query("INSERT INTO test (recipeName, ingredients, ingredients2, ingredients3, ingredients4, ingredients5, ingredients6, ingredients7, ingredients8, ingredients9) VALUES('$name', '$ingredients', '$ingredients2', '$ingredients3', '$ingredients4', '$ingredients5', '$ingredients6', '$ingredients7', '$ingredients8', '$ingredients9')") or die (mysql_error()); 
}
?>

感谢您的任何和所有提示/帮助。

Thanks for any and all tips/help.

BRR

推荐答案

对于那些谁不知道什么工作 - 这里是我想出了:

For those who wonder what worked - here is what I came up with:

<?php
header('Content-Type: text/html; charset=utf-8');

$hostname_ndb = "localhost";
$database_ndb = "test";
$username_ndb = "root";
$password_ndb = "root";
$ndb = mysql_pconnect($hostname_ndb, $username_ndb, $password_ndb) or trigger_error(mysql_error(),E_USER_ERROR); 
mysql_select_db($database_ndb);

$url = "http://localhost:8888/shakerecipes/recipes.json";
$json = file_get_contents($url);

$out = json_decode($json, true);

foreach($out["recipes"] as $recipe) { 
$name = addslashes($recipe[recipeName]); 
$ingredients= addslashes($recipe[ingredients]); 
$ingredients2 = addslashes($recipe[ingredients2]);
$ingredients3 = addslashes($recipe[ingredients3]);
$ingredients4 = addslashes($recipe[ingredients4]);
$ingredients5 = addslashes($recipe[ingredients5]);
$ingredients6 = addslashes($recipe[ingredients6]);
$ingredients7 = addslashes($recipe[ingredients7]);
$ingredients8 = addslashes($recipe[ingredients8]);
$ingredients9 = addslashes($recipe[ingredients9]);

mysql_query("INSERT INTO recipes (recipeName, ingredients, ingredients2, ingredients3, ingredients4, ingredients5, ingredients6, ingredients7, ingredients8, ingredients9) VALUES('$name', '$ingredients', '$ingredients2', '$ingredients3', '$ingredients4', '$ingredients5', '$ingredients6', '$ingredients7', '$ingredients8', '$ingredients9')") or die (mysql_error()); 

}
?>

这篇关于JSON到MYSQL - 是JSON格式的响应正确 - 循环是否正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆