Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:29:34+00:00 2026-05-22T15:29:34+00:00

OK – I am attempting to load a JSON response (from an external file

  • 0

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"}

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:29:34+00:00Added an answer on May 22, 2026 at 3:29 pm

    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()); 
    
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem with unsigned char. I am reading a PPM image file which has
I have a simple restful service that transforms a JAXB-anntotated beans to response XML
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
An extension to my previous question: Text cleaning and replacement: delete \n from a
I am getting the value from web service like &gt;&lt;&amp;&nbsp; etc. I want to
I am currently running into a problem where an element is coming back from
I am currently scraping some data from the internet and converting into xml documents.
I would like to count the length of a string with PHP. The string
I've got the following login script.. <?php $name = $_POST[name]; $password = $_POST[password]; $query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.