0

I already did to fetch data in JSON and insert into MySQL. But I got a problem if the data is like array in array. I tried many methods by myself but I dont know how to do it proper for

This is the index.php

<html>  
      <head>  
           <title>Media Monitoring</title> 

     <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">Import JSON File Data into Mysql Database in PHP</h3><br />
          <?php
          $connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
          $query = '';
          $table_data = '';
          $filename = "json.json";
          $data = file_get_contents($filename); //Read the JSON file in PHP
          $array = json_decode($data, true); //Convert JSON String into PHP Array
          foreach($array as $row) //Extract the Array Values by using Foreach Loop
          {
           $query .= "INSERT INTO employee(name, gender, gg) VALUES ('".$row["name"]."', '".$row["gender"]."', '".$row["gg"]."'); ";  // Make Multiple Insert Query 
           $table_data .= '
            <tr>
       <td>'.$row["name"].'</td>
       <td>'.$row["gender"].'</td>
       <td>'.$row["gg"].'</td>
      </tr>
           '; //Data for display on Web page
          }
          if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
    {
     echo '<h3>Imported JSON Data</h3><br />';
     echo '
      <table class="table table-bordered">
        <tr>
         <th width="45%">Name</th>
         <th width="10%">Gender</th>
         <th width="45%">Designation</th>
        </tr>
     ';
     echo $table_data;  
     echo '</table>';
          }




          ?>
     <br />
         </div>  
      </body>  
 </html> 

This is the editted

 <html>  
      <head>  
           <title>Media Monitoring</title> 

     <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">Import JSON File Data into Mysql Database in PHP</h3><br />
          <?php
          $connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
          $query = '';
          $table_data = '';
          $filename = "json.json";
          $data = file_get_contents($filename); //Read the JSON file in PHP
          $array = json_decode($data, true); //Convert JSON String into PHP Array
          foreach($array as $row) //Extract the Array Values by using Foreach Loop
          {
           $query .= "INSERT INTO employee(name, gender, gg) VALUES ('".$row["items""name"]."', '".$row["items"]["gender"]."', '".$row["items"].["gg"]."'); ";  // Make Multiple Insert Query 
           $table_data .= '
            <tr>
       <td>'.$row["name"].'</td>
       <td>'.$row["gender"].'</td>
       <td>'.$row["gg"].'</td>
      </tr>
           '; //Data for display on Web page
          }
          if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
    {
     echo '<h3>Imported JSON Data</h3><br />';
     echo '
      <table class="table table-bordered">
        <tr>
         <th width="45%">Name</th>
         <th width="10%">Gender</th>
         <th width="45%">Designation</th>
        </tr>
     ';
     echo $table_data;  
     echo '</table>';
          }




          ?>
     <br />
         </div>  
      </body>  
 </html> 

JSON Data that work

[
    {  
     "name": "Rusydi",  
     "gender": "Male",  
     "gg": "System Architect"  
    },  

    {  
     "name": "Hakim",  
     "gender": "Male",  
     "gg": "Conservation worker"  
    }
 ]

JSON Data Array in Array

{  
  "items": [
    {  
     "name": "Rusydi",  
     "gender": "Male",  
     "gg": "System Architect"  
    },  

    {  
     "name": "Hakim",  
     "gender": "Male",  
     "gg": "Conservation worker"  
    }
 ]
 }
4
  • 1
    change this line foreach($array as $row) to foreach($array['items'] as $row). Commented May 1, 2017 at 10:29
  • Yes its work. Tq Commented May 1, 2017 at 11:21
  • Glad to help you :) Commented May 1, 2017 at 11:22
  • But i got problem.. Why i can save data from json into MySQL but i cant save data from google custom search API Commented May 1, 2017 at 11:56

1 Answer 1

1

You can try this

 <html>  
      <head>  
           <title>Media Monitoring</title> 

     <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">Import JSON File Data into Mysql Database in PHP</h3><br />
          <?php
          $connect = mysqli_connect("localhost", "root", "", "test_db"); //Connect PHP to MySQL Database
          $connect->set_charset("utf8");
          $query = '';
          $table_data = '';
          $filename = "https://www.googleapis.com/customsearch/v1?key=API_KEY&cx=e838f67a0ac798369&q=Abir%20Abedin%20Khan";
          $data = file_get_contents($filename); //Read the JSON file in PHP
          $array = json_decode($data, true); //Convert JSON String into PHP Array
          foreach($array['items'] as $row) //Extract the Array Values by using Foreach Loop
          {
           $query .= "INSERT INTO  search_engine(title, blurb, url) VALUES ('".$row["title"]."', '".$row["snippet"]."', '".$row["link"]."'); ";  // Make Multiple Insert Query 
           $table_data .= '
            <tr>
       <td>'.$row["title"].'</td>
       <td>'.$row["snippet"].'</td>
       <td>'.$row["link"].'</td>
      </tr>
           '; //Data for display on Web page
          }
          if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
    {
     echo '<h3>Imported JSON Data</h3><br />';
     echo '
      <table class="table table-bordered">
        <tr>
         <th width="45%">Title</th>
         <th width="10%">Blurb</th>
         <th width="45%">Link</th>
        </tr>
     ';
     echo $table_data;  
     echo '</table>';
          }




          ?>
     <br />
         </div>  
      </body>  
 </html> 
1
  • This answer is not implement secure/stable querying practices and should not be used. Please read about prepared statements. Commented Nov 15, 2022 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.