How To Import JSON File Using PDO In PHP | CampCodes ...

How to Import JSON File Using PDO in PHP

July 9, 2020
import json file using pdo in php

How to Import JSON File Using PDO in PHP

In this tutorial, we will create an Import JSON File using PDO in PHP. This code can import the JSON file to the database server with the PDO query. The code uses a json_decode(); to break down the JSON format into a readable PHP array, then it will upload the data via for() loop to insert the data row by row to the database server. This is a user-friendly kind of program, feel free to modify it.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much more accessible.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server

And, this is the link for the bootstrap that I used for the layout design

Creating Database

Open your database web server then create a database name in it db_import, after that, click Import then locates the database file inside the folder of the application then click ok.

json file using pdo in php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

    $conn = new PDO( 'mysql:host=localhost;dbname=db_import', 'root', '');
        die("Fatal Error: Connection Failed!");

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Simple Import JSON File Using PDO</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-5">
            <form method="POST" action="upload.php" enctype="multipart/form-data">
                <div class="form-group">
                    <input type="file" name="filename" class="form-control"/>
                <div class="form-group">
                    <button name="upload" class="btn btn-primary form-control"><span class="glyphicon glyphicon-upload"></span> Upload</button>
        <div class="col-md-7">
            <table class="table table-bordered">
                <thead class="alert-info">
                        require 'conn.php';
                        $query = $conn->prepare("SELECT * FROM `member`");
                        while($fetch = $query->fetch()){
                        <td><?php echo $fetch['firstname']?></td>
                        <td><?php echo $fetch['lastname']?></td>
                        <td><?php echo $fetch['gender']?></td>
                        <td><?php echo $fetch['address']?></td>

Creating the Main Function

This code contains the main function of the application. This code will import the json file when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as upload.php.

    require 'conn.php';
        $file = explode(".", $_FILES['filename']['name']);
        $ext = end($file);
        if($ext == "json"){
            $data = file_get_contents($_FILES['filename']['tmp_name']);
            $array = json_decode($data, true);
                foreach($array as $row){
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $sql = "INSERT INTO `member` (`firstname`, `lastname`, `gender`, `address`) VALUES ('$row[firstname]', '$row[lastname]', '$row[gender]', '$row[address]')";
            }catch(PDOException $e){
                echo $e->getMessage();
            $conn = null;
            header("location: index.php");
            echo "<script>alert('Only JSON file is available')</script>";
            echo "<script>window.location = 'index.php'</script>";
There you have it we successfully created a Simple Import JSON File using PDO. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

PHP - Simple Import JSON File Using PDO Source Code 339.18 KB 28 downloads

Please go back to the description page. Just Click the Red Download Button on the...

Leave a Reply

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