Insert JSON data into MySQL database using PHP

In PHP, file_get_contents(), json_decode(), and mysqli_multi_query() functions help developers to read data in json file to insert into MySQL database.

Through this tutorial, we will learn how to import or Insert JSON file data to MySQL database using PHP.

How to Insert JSON data into MySQL database using PHP?

Use the following steps to import/insert JSON file data into MySQL database:

Step 1 – Create Database Configuration

Use the following PHP code to create a 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

Use 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

Use 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:

<!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>

Conclusion

Through this tutorial, we have learned how to import/Insert JSON file data into MySQL database using PHP or fetch/get data from JSON file and import it into the Mysql database using PHP.

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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