PHP Import JSON File data into MySQL database

Import JSON file data into MySQL database using PHP; Through this tutorial, we will learn how to import JSON file data into MySQL database using PHP or fetch data from JSON file and import it into the Mysql database using PHP. And as well, using the following tutorial, we will insert or import single or multiple JSON file data into MySQL database using PHP script or code.

PHP Import JSON File data into MySQL database

How to Insert JSON File data into MySQL database using PHP

Use the following steps to import or insert JSON file data into MySQL database using PHP; as follows:

  • Step 1 – Create Database Configuration
  • Step 2 – Read Json Data From File
  • Step 3 – Insert JSON data Into MySQL Database

Step 1 – Create Database Configuration

Use the following php code to create connection from mysql database; as follows:

1
2
3
4
5
6
// 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

Use the following php code to read json data from file; as follows:

1
2
3
4
5
6
7
8
// 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

Use the following php code to insert json file data into MySQL database; as follows:

1
2
3
4
5
6
7
// 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:

follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html>
<head>
<script src=
</script>
<link rel="stylesheet" href=
<script src=
</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>