insert json data into mysql – How to Insert JSON data into MySQL using PHP?

insert json data into mysql using $obj = json_decode($json,true); MySQL INSERT statement: It is used to insert data into the database.

insert json data into mysql

Insert JSON data MySQL – There is step by step process to insert json data into Database.

 $json = file_get_contents('php://input');
 $obj = json_decode($json,true);

Read the JSON file in PHP
read the json file contents

<?php
    $jsondata = file_get_contents('members.json');
?>

Convert JSON String into PHP Array
convert json object to php associative array

<?php
    $members_data = json_decode($membersjsondata, true);
?>

How to Insert JSON data into MySQL using PHP?

index.php

<html>
<head>
<title>Insert JSON Data - www.pakainfo.com</title>
</head>
<body>
<?php 

$memData = '{
"member":{ "name":"Rakesh", "country":"India", "MobileNumber":982560000 }
}';  

// Database connection
$link = new mysqli('localhost', 'root', '[email protected]#$df565f4', 'pakainfo_v1');

// Insert data Query
$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Rakesh', '$memData')";

if ($link->query($sql) === TRUE) {
  echo "Insert your JSON record successfully";
} 
?>
</body>
</html>

don’t miss : How To Insert Json Data Into Mysql Using Php?

Also Read This πŸ‘‰   Fix: file_get_contents() wrapper is disabled error in PHP

Store JSON data in MySQL Database

index.php

<html>
<head>
<title>Store JSON Data - www.pakainfo.com</title>
</head>
<body>
<?php 
$memData = '{
"member":{ "name":"Rakesh", "country":"India", "MobileNumber":9825632232 }
}';
  $data = json_decode($memData);
  $singledata = $data->member->name;
  
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "member";

// Database connection
$link = new mysqli($servername, $username, $password, $dbname);

$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Rakesh', '$singledata')";

if ($link->query($sql) === TRUE) {
  echo "Member Insert data only single successfully";
}  
?>
</body>
</html>

How to Insert Multiple JSON Data into MySQL Database in Php?

index.php

<html>
<head>
<title>Insert Multiple JSON Data - www.pakainfo.com</title>
</head>
<body>
<?php 

$memData = '{
"Member":{ "name":"Jonny", "country":"CANADA", "MobileNumber":524587777 },
"member":{ "name":"Gussy", "country":"USA", "MobileNumber":9856452100}
}';  

// Database connection
$link = new mysqli('localhost', 'root', 'Fg5454#$ff', 'pakainfo_v1');

// Insert data Query
$sql = "INSERT INTO member_table ( name, jsondata )
VALUES ('Multidata', '$memData')";

if ($link->query($sql) === TRUE) {
  echo "Insert your Multiple Member JSON record successfully";
} 
?>
</body>
</html>

I hope you get an idea about insert json data into mysql.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.