douqi3064 2018-06-08 01:50
浏览 174
已采纳

将API请求中的JSON数据插入MySQL数据库

Previously I have posted a similar question on passing JSON data to drop down menu using PHP. How can i arrange data array from API request into HTML SELECT OPTION LIST?

Right now I would like to INSERT the JSON datat in to MySQL database

Below is my api_redcap.php code for calling the data from other web server:

<?php 
     $data = array(
          'token' => '4B0D42AB9D061C0FADD724D2E908349D',
          'content' => 'report',
          'format' => 'json',
          'report_id' => '71',
          'rawOrLabel' => 'label',
          'rawOrLabelHeaders' => 'label',
          'exportCheckboxLabel' => 'false',
          'returnFormat' => 'json'); 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://redcap-virtualbox/redcap/api/');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_VERBOSE, 0);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_AUTOREFERER, true);

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, '', '&'));

$output = curl_exec($ch);

$json = json_decode($output);    
?>

Below is my output from api_redcap.php.:

[{"project_code":"16001","dept":"Orthopaedic Surgery (ORTHO)","group":"","name":"Jameson Lua Yao Chung"},{"project_code":"16002","dept":"Rheumatology Allergy & Immunology (RAI)","group":"","name":"Koh Ee Tzun"},{"project_code":"16003","dept":"Orthopaedic Surgery (ORTHO)","group":"","name":"Ang Wei Luong"},{"project_code":"16004","dept":"Rheumatology Allergy & Immunology (RAI)","group":"","name":"Lim Xin Rong"},{"project_code":"16005","dept":"Physiotherapy (PT)","group":"","name":"Li Kun Man"}]

My table name is called PROJECT_CODE in MySQL database and the attributes are as follow:

Table Name - PROJECT_CODE Field Name: PROJECT_code, DEPARTMENT, REQUESTOR,

I would like to insert the project_code, dept and name variables from JSON into the MySQL table fields respectively. Please help. Thanks

  • 写回答

1条回答 默认 最新

  • dpfz27768 2018-06-08 02:50
    关注
    $json = json_decode($output, true);
    foreach($json as $val)
    {
        $project_code = $val['project_code'];
        $dept = $val['dept'];
        $name = $val['name'];
    
        /*
        * Generate insert query as below
        * "INSERT INTO MyGuests (project_code, dept, name) VALUES ($project_code, $dept, $name);";
        */
    
    }
    

    Also, you can insert multiple record in single loop Please check below reference link https://www.w3schools.com/php/php_mysql_insert_multiple.asp

    In reference link 3 different methods given,so, you can use any one whatever suitable for you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证