PHP MySQLi How To Save JSON Data To Database Tutorial

Reading Time: 6 minutes
2,341 Views

Inside this article we will see the concept i.e PHP MySQLi How To Save JSON file to Database. Article contains classified information. It will give the complete idea of parsing a JSON file and save into PHP MySQLi database.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you will learn only reading JSON file, you can use the same concept in data seeding to database via JSON file. This is step by step tutorial in PHP about JSON file data seeding inside mysql database.

In this tutorial we will use JSON file to seed data into database table using PHP MySQLi.

Learn More –

Let’s get started.

Create Database & Table

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE php_applications;

Inside this database, we need to create a table.

Table we need – countries

CREATE TABLE `countries` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(120) NOT NULL,
 `sortname` varchar(10) NOT NULL,
 `phonecode` varchar(20) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Dummy Data for Application

As we have table countries in which columns as id, name, sortname, phonecode.

We will take a JSON file in which properties of each object will be ID, Sortname, Name & Phonecode. Rest we need data into it.

When you will download the given file it will be in .txt format. Rename file into .json and place this countries.json file inside folder.

Application Folder Structure

You need to create a folder structure to develop this application in PHP and MySQLi. Have a look the files and folders inside this application –

Create a folder with name php-mysqli-json and create these 2 files into it.

Database Configuration

Open dbconfig.php file from folder. Add these lines of code into it.

<?php
/*
 @Author: Sanjay Kumar
 @Project: PHP & MySQLi How To Save JSON Data To Database Tutorial
 @Email: onlinewebtutorhub@gmail.com
 @Website: https://onlinewebtutorblog.com/
*/

// Database configuration
$host   = "localhost";
$dbuser = "admin";
$dbpass = "Admin@123";
$dbname = "php_applications";

// Create database connection
$conn = new mysqli($host, $dbuser, $dbpass, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

Application Programming

Open index.php file and write this following code into it.

<?php
/*
 @Author: Sanjay Kumar
 @Project: PHP & MySQLi How To Save JSON Data To Database Tutorial
 @Email: onlinewebtutorhub@gmail.com
 @Website: https://onlinewebtutorblog.com/
*/

// Include the database file
require 'dbconfig.php';

$data = [];

$json = file_get_contents("countries.json");
$countries_data = json_decode($json);

foreach ($countries_data->countries as $key => $value) {
    $data[] = [
        "sortname" => $value->sortname,
        "name" => $value->name,
        "phonecode" => $value->phoneCode
    ];
}

// echo "<pre>";
// print_r($data);

if (!empty($data) && count($data) > 0) {

    foreach ($data as $country) {

        $stmt = $conn->prepare("INSERT INTO countries (name, sortname, phonecode) VALUES(?, ?, ?)");
        $stmt->bind_param("sss", $country['name'], $country['sortname'], $country['phonecode']);
        $stmt->execute();
    }
}

Application Testing

Now,

URL: http://localhost/php-mysqli-json/index.php

Back to database table, you should see inside countries table –

Download Complete Source Code

We hope this article helped you to PHP MySQLi How To Save JSON Data To Database Table Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.