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

vbopmzt1  于 2022-11-19  发布在  Mysql
关注(0)|答案(1)|浏览(175)

我已经在JSON中提取了数据并插入到MySQL中。但是如果数据像数组中的数组一样,我会遇到一个问题。我自己尝试了很多方法,但我不知道如何正确地进行
这是一个很好的例子。

  1. <html>
  2. <head>
  3. <title>Media Monitoring</title>
  4. <style>
  5. .box
  6. {
  7. width:750px;
  8. padding:20px;
  9. background-color:#fff;
  10. border:1px solid #ccc;
  11. border-radius:5px;
  12. margin-top:100px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="container box">
  18. <h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
  19. <?php
  20. $connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
  21. $query = '';
  22. $table_data = '';
  23. $filename = "json.json";
  24. $data = file_get_contents($filename); //Read the JSON file in PHP
  25. $array = json_decode($data, true); //Convert JSON String into PHP Array
  26. foreach($array as $row) //Extract the Array Values by using Foreach Loop
  27. {
  28. $query .= "INSERT INTO employee(name, gender, gg) VALUES ('".$row["name"]."', '".$row["gender"]."', '".$row["gg"]."'); "; // Make Multiple Insert Query
  29. $table_data .= '
  30. <tr>
  31. <td>'.$row["name"].'</td>
  32. <td>'.$row["gender"].'</td>
  33. <td>'.$row["gg"].'</td>
  34. </tr>
  35. '; //Data for display on Web page
  36. }
  37. if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
  38. {
  39. echo '<h3>Imported JSON Data</h3><br />';
  40. echo '
  41. <table class="table table-bordered">
  42. <tr>
  43. <th width="45%">Name</th>
  44. <th width="10%">Gender</th>
  45. <th width="45%">Designation</th>
  46. </tr>
  47. ';
  48. echo $table_data;
  49. echo '</table>';
  50. }
  51. ?>
  52. <br />
  53. </div>
  54. </body>
  55. </html>

这是编辑过的

  1. <html>
  2. <head>
  3. <title>Media Monitoring</title>
  4. <style>
  5. .box
  6. {
  7. width:750px;
  8. padding:20px;
  9. background-color:#fff;
  10. border:1px solid #ccc;
  11. border-radius:5px;
  12. margin-top:100px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="container box">
  18. <h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
  19. <?php
  20. $connect = mysqli_connect("localhost", "root", "", "accounts"); //Connect PHP to MySQL Database
  21. $query = '';
  22. $table_data = '';
  23. $filename = "json.json";
  24. $data = file_get_contents($filename); //Read the JSON file in PHP
  25. $array = json_decode($data, true); //Convert JSON String into PHP Array
  26. foreach($array as $row) //Extract the Array Values by using Foreach Loop
  27. {
  28. $query .= "INSERT INTO employee(name, gender, gg) VALUES ('".$row["items""name"]."', '".$row["items"]["gender"]."', '".$row["items"].["gg"]."'); "; // Make Multiple Insert Query
  29. $table_data .= '
  30. <tr>
  31. <td>'.$row["name"].'</td>
  32. <td>'.$row["gender"].'</td>
  33. <td>'.$row["gg"].'</td>
  34. </tr>
  35. '; //Data for display on Web page
  36. }
  37. if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
  38. {
  39. echo '<h3>Imported JSON Data</h3><br />';
  40. echo '
  41. <table class="table table-bordered">
  42. <tr>
  43. <th width="45%">Name</th>
  44. <th width="10%">Gender</th>
  45. <th width="45%">Designation</th>
  46. </tr>
  47. ';
  48. echo $table_data;
  49. echo '</table>';
  50. }
  51. ?>
  52. <br />
  53. </div>
  54. </body>
  55. </html>

工作的JSON数据

  1. [
  2. {
  3. "name": "Rusydi",
  4. "gender": "Male",
  5. "gg": "System Architect"
  6. },
  7. {
  8. "name": "Hakim",
  9. "gender": "Male",
  10. "gg": "Conservation worker"
  11. }
  12. ]

数组中的JSON数据数组

  1. {
  2. "items": [
  3. {
  4. "name": "Rusydi",
  5. "gender": "Male",
  6. "gg": "System Architect"
  7. },
  8. {
  9. "name": "Hakim",
  10. "gender": "Male",
  11. "gg": "Conservation worker"
  12. }
  13. ]
  14. }
rqqzpn5f

rqqzpn5f1#

你可以试试这个

  1. <html>
  2. <head>
  3. <title>Media Monitoring</title>
  4. <style>
  5. .box
  6. {
  7. width:750px;
  8. padding:20px;
  9. background-color:#fff;
  10. border:1px solid #ccc;
  11. border-radius:5px;
  12. margin-top:100px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="container box">
  18. <h3 align="center">Import JSON File Data into Mysql Database in PHP</h3><br />
  19. <?php
  20. $connect = mysqli_connect("localhost", "root", "", "test_db"); //Connect PHP to MySQL Database
  21. $connect->set_charset("utf8");
  22. $query = '';
  23. $table_data = '';
  24. $filename = "https://www.googleapis.com/customsearch/v1?key=API_KEY&cx=e838f67a0ac798369&q=Abir%20Abedin%20Khan";
  25. $data = file_get_contents($filename); //Read the JSON file in PHP
  26. $array = json_decode($data, true); //Convert JSON String into PHP Array
  27. foreach($array['items'] as $row) //Extract the Array Values by using Foreach Loop
  28. {
  29. $query .= "INSERT INTO search_engine(title, blurb, url) VALUES ('".$row["title"]."', '".$row["snippet"]."', '".$row["link"]."'); "; // Make Multiple Insert Query
  30. $table_data .= '
  31. <tr>
  32. <td>'.$row["title"].'</td>
  33. <td>'.$row["snippet"].'</td>
  34. <td>'.$row["link"].'</td>
  35. </tr>
  36. '; //Data for display on Web page
  37. }
  38. if(mysqli_multi_query($connect, $query)) //Run Mutliple Insert Query
  39. {
  40. echo '<h3>Imported JSON Data</h3><br />';
  41. echo '
  42. <table class="table table-bordered">
  43. <tr>
  44. <th width="45%">Title</th>
  45. <th width="10%">Blurb</th>
  46. <th width="45%">Link</th>
  47. </tr>
  48. ';
  49. echo $table_data;
  50. echo '</table>';
  51. }
  52. ?>
  53. <br />
  54. </div>
  55. </body>
  56. </html>
展开查看全部

相关问题