how to insert json data into mysql using php

how to insert json data into mysql using php : PHP Script read JSON file contents and Insert into MySQL.

how to insert json data into mysql using php

first of all, created a Multi JSON file and write the file by the PHP source code. Creating Data in the JSON Field. Now, you are going to make products to add to the database using INSERT INTO and VALUES.

Do you want to know how a JSON file looks like? Well here is the sample json Data File.

{
    "memberid": "SJ011MS",
    "individual": {
        "name": "Virat Kohali",
        "gender": "Male",
        "age": "32",
        "apartment": {
            "streetapartment": "8 19th Street",
            "city": "Gujrat",
            "state": "GJ",
            "zip": "10038"
        }
    },
    "account": {
        "appointment": "Manager Sinors",
        "section": "Finance"
    }
}

Step 1: Connect PHP to MySQL Database

<?php
    $link = mysql_connect("username","password","") or die('Could not connect: ' . mysql_error());
    mysql_select_db("members", $link);
?>

Step 2: Read the JSON file in PHP

<?php
    //read the json file contents
    $result_content = file_get_contents('memberdetails.json');
?>

Step 3: Convert JSON String into PHP Array

<?php
    //convert json object to php associative array
    $row_result = json_decode($result_content, true);
?>

Step 4: Extract the Array Values

<?php
    //get the members details
    $id = $row_result['memberid'];
    $name = $row_result['individual']['name'];
    $gender = $row_result['individual']['gender'];
    $age = $row_result['individual']['age'];
    $streetapartment = $row_result['individual']['apartment']['streetapartment'];
    $city = $row_result['individual']['apartment']['city'];
    $state = $row_result['individual']['apartment']['state'];
    $zip = $row_result['individual']['apartment']['zip'];
    $appointment = $row_result['account']['appointment'];
    $section = $row_result['account']['section'];
?>

Step 5: Insert JSON to MySQL Database with PHP Code

<?php
    //insert into mysql table
    $sql = "INSERT INTO tbl_member(memberid, membername, gender, age, streetapartment, city, state, zip, appointment, section)
    VALUES('$id', '$name', '$gender', '$age', '$streetapartment', '$city', '$state', '$zip', '$appointment', '$section')";
    if(!mysql_query($sql,$link))
    {
        die('Error : ' . mysql_error());
    }
?>

Don’t Miss : how to store json data in mysql using php

Read Also:  sql joins with examples

Insert JSON data into MySQL using php

Complete PHP Script (index.php)

<?php
    //connect to mysql db
    $link = mysql_connect("username","password","") or die('Could not connect: ' . mysql_error());
    //connect to the members database
    mysql_select_db("members", $link);

    //read the json file contents
    $result_content = file_get_contents('memberdetails.json');
    
    //convert json object to php associative array
    $row_result = json_decode($result_content, true);
    
    //get the members details
    $id = $row_result['memberid'];
    $name = $row_result['individual']['name'];
    $gender = $row_result['individual']['gender'];
    $age = $row_result['individual']['age'];
    $streetapartment = $row_result['individual']['apartment']['streetapartment'];
    $city = $row_result['individual']['apartment']['city'];
    $state = $row_result['individual']['apartment']['state'];
    $zip = $row_result['individual']['apartment']['zip'];
    $appointment = $row_result['account']['appointment'];
    $section = $row_result['account']['section'];
    
    //insert into mysql table
    $sql = "INSERT INTO tbl_member(memberid, membername, gender, age, streetapartment, city, state, zip, appointment, section)
    VALUES('$id', '$name', '$gender', '$age', '$streetapartment', '$city', '$state', '$zip', '$appointment', '$section')";
    if(!mysql_query($sql,$link))
    {
        die('Error : ' . mysql_error());
    }
?>

memberdetails.json

{
    "memberid": "SJ011MS",
    "individual": {
        "name": "Virat Kohali",
        "gender": "Male",
        "age": "28",
        "apartment": {
            "streetapartment": "8 19th Ambuja",
            "city": "Rajkot",
            "state": "GJ",
            "zip": "360002"
        }
    },
    "account": {
        "appointment": "Deputy General",
        "section": "Finance"
    }
}


I hope you get an idea about how to insert json data into mysql using php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.