<!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"
>
Geeks
for
Geeks Import JSON
data into database
</h3><br />
<?php
$connect
= mysqli_connect(
"localhost"
,
"root"
,
""
,
"test"
);
$query
=
''
;
$table_data
=
''
;
$filename
=
"college_subjects.json"
;
$data
=
file_get_contents
(
$filename
);
$array
= json_decode(
$data
, true);
foreach
(
$array
as
$row
) {
$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>
';
}
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>