How to Import JSON File Data Into MySQL Database Using PHP?
Teams applying “How to Import JSON File Data Into MySQL Database Using PHP?” in a production project can use this reference page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official MySQL website and test it against the project’s actual database and runtime.
Hi Dev,
This tutorial is focused on how to import json file data into mysql database using php. I would like to share with you insert json data into mysql using php. This article goes in detailed on php import json file data into mysql database. you'll learn import data from json file to php and mysql.
The following tutorial, we will insert or import single or multiple JSON file data into MySQL database using PHP script or code.
Step 1: Create Database Configuration
The following php code to create connection from mysql database; as follows:
// Server name => localhost
// Username => root
// Password => empty
// Database name => test
// Passing these 4 parameters
$connect = mysqli_connect("localhost", "root", "", "test");
Step 2: Read Json Data From File
The following php code to read json data from file; as follows:
// json file name
$filename = "college_subjects.json";
// Read the JSON file in PHP
$data = file_get_contents($filename);
// Convert the JSON String into PHP Array
$array = json_decode($data, true);
Step 3: Insert JSON data Into MySQL Database
The following php code to insert json file data into MySQL database; as follows:
/ Database query to insert data
// into database Make Multiple
// Insert Query
$query .= "INSERT INTO student VALUES ('".$row["name"]."', '".$row["gender"]."', '".$row["subject"]."'); ";
The full source code of How to Insert JSON File data into MySQL database using PHP; as follows:
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js">
</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">
</script>
<style>
.box {
width: 750px;
padding: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
margin-top: 100px;
}
</style>
</head>
<body>
<div class="container box">
<h3 align="center">How to Insert JSON File data into MySQL database using PHP</h3><br/>
<?php
// Server name => localhost
// Username => root
// Password => empty
// Database name => test
// Passing these 4 parameters
$connect = mysqli_connect("localhost", "root", "", "test");
$query = '';
$table_data = '';
// json file name
$filename = "college_subjects.json";
// Read the JSON file in PHP
$data = file_get_contents($filename);
// Convert the JSON String into PHP Array
$array = json_decode($data, true);
// Extracting row by row
foreach($array as $row) {
// Database query to insert data
// into database Make Multiple
// Insert Query
$query .="INSERT INTO student VALUES ('".$row["name"]."', '".$row["gender"]."', '".$row["subject"]."'); ";
$table_data .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["gender"].'</td>
<td>'.$row["subject"].'</td>
</tr>'; // Data for display on Web page
}
if(mysqli_multi_query($connect, $query)) {
echo '<h3>Inserted JSON Data</h3><br/>';
echo '
<table class="table table-bordered">
<tr>
<th width="45%">Name</th>
<th width="10%">Gender</th>
<th width="45%">Subject</th>
</tr>';
echo $table_data; echo
'</table>';
}
?><br/>
</div>
</body>
</html>
I hope it could help you...
- How to Increase max_connections in MySQL?
- How to Install the PHP PDO MySQL Extension on Ubuntu 22.04?
- How to Install PHP MySQL on Linux Ubuntu?
- How to Install LEMP Stack Nginx, MySQL, PHP on Ubuntu?
- How to Change MySQL Root User Password Ubuntu?
- PHP 8 MySQL Form Validation Example
- How to Select First Row Only in PHP MySQL?
- How To Backup MySQL Database Using PHP Example