Insert JSON data MySQL

Today, We want to share with you Insert JSON data MySQL.In this post we will show you how to store json data in mysqli using php, hear for store json data in mysqli.

how to insert json data into mysql

we will give you demo and example for implement.In this post, we will learn about Google API Line Chart with JSON PHP with an example.

Step: 1 Create Database and Table

//create  database
CREATE DATABASE members_list;
 
USE members_list;
 
//create  table JSON
CREATE TABLE json(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
firstName CHAR( 50 ) NOT NULL ,
lastName CHAR( 50 ) NOT NULL

Step: 2 Create a JSON file

{"FirstName":"krishna", "LastName":"Kakadiya"}

Step: 3 PHP Script read JSON file contents and Inserts into MySQL

<?php
$host="localhost";
$user="root";
$pass="CD*********";
$db="members_list";
$connect= new mysqli($host,$user,$pass,$db) or die("ERROR:could not connect to the database!!!");

$fo=fopen("json_file.json","r");
$fr=fread($fo,filesize("json_file.json"));
$array=json_decode($fr,true);

//To display all values from JSON file
//print_r($array);
 
$query="insert into json values('','$array[FirstName]','$array[LastName]')";

$connect->query($query);

echo "Data Imported Sucessfully from JSON!";
?>

how to inserts json data into mysql using php?

<?php 

$josndata = '{
"member":{ "name":"Rekha", "department":"MCA BASE", "ContactNo":9XXXXXXXX5 }
}';  

// Database connection
$conn = new mysqli('localhost', 'username', 'password', 'databasename');

// Inserts data Query
$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Rekha', '$josndata')";

if ($conn->query($sql) === TRUE) {
  echo "Insert your JSON record successfully";
} 
?>


I hope you get an idea about Inserts JSON data MySQL.
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.