Home » Insert JSON data MySQL

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.

Read Also:  PHP MySQL: Querying fetch Data from Database

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.

Read Also:  Laravel 6 JQuery Ajax Post Request Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to Force Download Files Using Laravel 5.7
  2. Read Also:  how to store json data in mysql using php?
  3. Read Also:  PHP Domdocument Get selectors Class Tags Table img Elements
  4. Read Also:  Laravel Roles Permissions Example Tutorial From Scratch
  5. Read Also:  PHP Laravel delete files from Public Folder
  6. Read Also:  PHP Loop: For, ForEach, While, Do While [Example - for loop in php]
  7. Read Also:  Laravel Calculate multiple totals in conditional aggregates
  8. Read Also:  Laravel Create Table using Migration and model
  9. Read Also:  [SOLUTIONS] 500 INTERNAL SERVER ERROR PHP
  10. Read Also:  [SOLUTIONS] 500 INTERNAL SERVER ERROR PHP

Leave a Comment

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