【问题标题】:Fetch Data Array in Array From JSON and Insert Into MySQL从 JSON 中获取数组中的数据数组并插入 MySQL
【发布时间】:2017-09-28 18:46:27
【问题描述】:

我已经以 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> 

这是编辑过的

 <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 数据

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

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

数组中的JSON数据数组

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

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

【问题讨论】:

  • 将此行 foreach($array as $row) 更改为 foreach($array['items'] as $row)
  • 是的,它的工作。时长
  • 很高兴为您提供帮助:)
  • 但我遇到了问题.. 为什么我可以将数据从 json 保存到 MySQL 但我不能从 google 自定义搜索 API 保存数据

标签: php mysql arrays json


【解决方案1】:

你可以试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2015-02-24
    • 1970-01-01
    • 2017-03-19
    • 2012-06-06
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多