Monday 19 September 2016

Fetch JSON Data Insert into Mysql table in PHP

Fetch JSON Data  Insert into MySQL table in PHP: Hello friends today we are going to learn the very basics of JSON data, MySQL and PHP that we will get the JSON data and then read JSON through the PHP script and after that insert the JSON formatted data into the MySQL database.

What are JSON and data format of JSON File ?

JSON is the abbreviation of JavaScript Object Notation is a lightweight data-interchange format that is used for the storage of data. More easy to read the data then the data is in the XML format. JSON type of data is supported all popular browser nowadays.

Example of JSON Data Format

The is the JSON format where every each entry is the pair of key and value in the object format. Where file name is myson.json.
[
    {
    "name": "Mike",
    "Age": "23",
    "Gender": "Male",
    "Country": "U.S"      
},
{
    "name": "Thomous",
    "Age": "21",
    "Gender": "Male",
    "Country": "U.S"      
},
{
    "name": "Johan",
    "Age": "22",
    "Gender": "Male",
    "Country": "U.S"      
}
]

This is the JSON formatted data  that is about the persons whose names, ages, genders, and countries are mentioned here we are going to insert into the MySQL database. In next steps, we will read the data and insert into the MySQL database table.

Step 1: Connect PHP to MySQL Database

Now the first step is we have to connect the PHP to MySQL by using the mysqli_connect()  function. So that we can insert our data into the MySQL database easily.
<?php  
$connect = mysqli_connect('localhost','root','');
mysqli_select_db($connect, 'json_db');
?>
 This PHP code will connect the PHP to MySQL database where the database name is "json_db" and then we will get the JSON data and insert into the table.

Step 2: Read the JSON file using PHP

Next step is to read the JSON file data and put that in the PHP variable so that we can read and manipulate the data according to our requirements. Well in PHP getting the data from the file to PHP variable is simple by using the file_get_contents() this function will read the whole JSON file and return the string. See usage in PHP 
<?php
    //read the json file contents
    $jsondata = file_get_contents('myjson.json');
?>
Where myjson.json is the JSON file that contains the content. $jsondata is the variable that contains the JSON file data. You can see by printing the data by just 
echo"<Pre>";print_r($jsondata); exit;
 This will print the JSON string.

Step 3: Convert JSON String to Php Array

Until we do not convert the string to PHP array either associative or any other type of array we can not manipulate the operations on the data so for that we are going to convert the string to the associative array we use the PHP function json_decode().
<?php
    //convert json object to php associative array
    $data = json_decode($jsondata, true);
?>
First parameter $jsondata is the variable that contains the string which is decode to an array and  the second true is used to convert into an associative array. If we print the $data then it is converted into associative array see the image below.
Fetch JSON Data & Insert into Mysql table in PHP

Here you can see the PHP coding directory files and the flow of the system. See the image below.

Fetch JSON Data & Insert into Mysql table in PHP

Step 4: Extract the Array Values

Now we will extract the array elements one by one and the insert that data elements into the MySQL database. Hope you are understanding the things right away. See the code.
<?php
foreach($data as $d_key => $d_val){              
            $id= $i++;
            $name = $d_val['name'];
            $age = $d_val['Age'];
            $gender = $d_val['Gender'];
            $country = $d_val['Country'];
}
?>
This is an extraction of an array into another array with the indexing and is used to insert data to MySQL database as my database name is json_db and I am now creating the table I attached the image below see and you can create your own data types of table.

Fetch JSON Data & Insert into Mysql table in PHP

Step 5: Insert JSON to MySQL Database with PHP Code

Now we have to insert the JSON data to the MySQL database using the PHP so that we can save our JSON data to the database and then can perform operations on that data. Here is the code of all the program.
<?php
$connect = mysqli_connect('localhost','root','');
mysqli_select_db($connect, 'json_db');
    //read the json file contents
    $jsondata = file_get_contents('myjson.json');
    //convert json object to php associative array
    $data = json_decode($jsondata, true);
    $i = 0;
    foreach($data as $d_key => $d_val){              
            $id= $i++;
            $name = $d_val['name'];
            $age = $d_val['Age'];
            $gender = $d_val['Gender'];
            $country = $d_val['Country'];      
     
        $query = "insert into person_info(name, age, gender, country) "
                . "values('$name','$age','$gender','$country')";
//        echo"<pre>";print_r($query);exit;
        if(mysqli_query($connect, $query)){
            echo "Records added successfully.";
        } else{
            echo "ERROR: Could not able to execute .$query " . mysqli_error($connect);
        }//end of else
    }//end of foreach loop      
?>
You can see the image of code and then the data that is inserted into the database so that all the things just damn clear to you.

Fetch JSON Data & Insert into Mysql table in PHP


Fetch JSON Data & Insert into Mysql table in PHP

 This is it. I hope this tutorial will help you if you people get any type of issue you can contact here and ask a question we will follow up and gave feedback soon. You can search with this query how to insert JSON data into MySQL using PHP, insert JSON into MySQL java, insert JSON into MySQL-python, store JSON data in MySQL, how to store JSON data in MySQL using java, JSON to MySQL table, insert JSON into MySQL, MySQL select JSON and much more and you will get the reading material of other websites. You can further read about earn money from the internet.
How you can earn money from the internet...!

This Post Was Last Updated On September 30, 2016, By Author: Mahira Khan.

0 comments:

Post a Comment

Item Reviewed: Fetch JSON Data Insert into Mysql table in PHP Rating: 5 Reviewed By: admin