Php 从JSON获取数组中的数据数组并插入MySQL

Php 从JSON获取数组中的数据数组并插入MySQL,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我已经用JSON获取数据并插入MySQL了。但是如果数据像数组中的数组,我就遇到了一个问题。我自己尝试了很多方法,但我不知道如何正确地为自己做 这是index.php <html> <head> <title>Media Monitoring</title> <style> .box { width:750px; padding:20px; b

我已经用JSON获取数据并插入MySQL了。但是如果数据像数组中的数组,我就遇到了一个问题。我自己尝试了很多方法,但我不知道如何正确地为自己做

这是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> 
数组中的JSON数据数组

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

    {  
     "name": "Hakim",  
     "gender": "Male",  
     "gg": "Conservation worker"  
    }
 ]
 }
你可以试试这个

 <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=AIzaSyCfaTE3WZtumt7gPas7Ey2GTKqWoJ2oH1M&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> 

媒体监测
盒
{
宽度:750px;
填充:20px;
背景色:#fff;
边框:1px实心#ccc;
边界半径:5px;
边缘顶部:100px;
}
将JSON文件数据导入PHP中的Mysql数据库


将此行更改为
foreach($row为数组)
foreach($row为数组['items'])
。是的,这是有效的。(很高兴帮助你。)但我有问题。。为什么我可以将json中的数据保存到MySQL中,但无法从google自定义搜索API中保存数据
{  
  "items": [
    {  
     "name": "Rusydi",  
     "gender": "Male",  
     "gg": "System Architect"  
    },  

    {  
     "name": "Hakim",  
     "gender": "Male",  
     "gg": "Conservation worker"  
    }
 ]
 }
 <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=AIzaSyCfaTE3WZtumt7gPas7Ey2GTKqWoJ2oH1M&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>