how to store json data in mysql using php?

Today, We want to share with you how to store json data in mysql using php.In this post we will show you import json to mysql using php script, hear for json to mysql php.

fetch json data and insert into mysql table in php

we will give you demo and example for implement.In this post, we will learn about PHP MySQLi Database Connection with an example.

Step 1: Read the JSON file in PHP

read the json file contents

$memberjsondata = file_get_contents('members.json');

Step 2: Convert JSON String into PHP Array

convert json object to php associative array

$data = json_decode($memberjsondata, true);

Step 3: Extract the Array Values in Variables

get the member details

$id = $data['memberId'];
$name = $data['peopleinfo']['name'];
$age = $data['peopleinfo']['age'];
$streetaddress = $data['peopleinfo']['address']['streetaddress'];
$task = $data['peopleinfo']['address']['task'];
$branch = $data['peopleinfo']['address']['branch'];
$postalcode = $data['peopleinfo']['address']['postalcode'];

Step 4: Insert JSON to MySQL Database with PHP Code

insert into mysql table

$sql = "INSERT INTO tbl_members(memberId, name, age, streetaddress, task, branch, postalcode)
    VALUES('$id', '$name', '$age', '$streetaddress', '$task', '$branch', '$postalcode')";
if(!mysqli_query($con, $sql))
    die('Error : ' . mysql_error());


    "memberId": "MT00985",
    "peopleinfo": {
        "name": "Rahul Mehta",
        "age": "32",
        "address": {
            "streetaddress": "9 12th Street",
            "task": "Komidaiya",
            "branch": "KD",
            "postalcode": "360002"

I hope you get an idea about how to store json data in mysql using php.
I would like to have feedback on my 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.