Home » how to store json data in mysql using php?

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.

Read Also:  Insert JSON data MySQL

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());
}

members.json

{
    "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 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.

Read Also:  AngularJS Login Script using PHP MySQL

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Files Uploading on Amazon S3 server using Laravel 5.4 Part-1
  2. Read Also:  Working with datetime in PHP Examples
  3. Read Also:  Laravel Eloquent Multiple WHERE clauses
  4. Read Also:  PHP MYSQLi datetime format insert into mysql
  5. Read Also:  Bootstrap datepicker example text input with date format
  6. Read Also:  PHP wordpress get timezone Example - MySQL
  7. Read Also:  Multiple Image upload preview remove option
  8. Read Also:  PHP MySQLi Database Connection
  9. Read Also:  Laravel Multiple Authentication Example
  10. Read Also:  Bootstrap datepicker example text input with date format

Leave a Comment

Your email address will not be published. Required fields are marked *