首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON数据从API请求插入到MySQL数据库中

将JSON数据从API请求插入到MySQL数据库中
EN

Stack Overflow用户
提问于 2018-06-08 01:50:38
回答 1查看 2.2K关注 0票数 0

在此之前,我曾发布过一个类似的问题,即如何使用PHP传递JSON数据以下拉菜单。How can i arrange data array from API request into HTML SELECT OPTION LIST?

现在我想将JSON数据插入到MySQL数据库中

下面是我从其他web服务器调用数据的api_redcap.php代码:

代码语言:javascript
复制
<?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);    
?>

下面是我来自api_redcap.php的输出:

{"project_code":"16001“、”科“:”骨科外科(骨科)“、”团体“:”“、”名称“:”詹姆逊·卢亚耀宗“}、{"project_code":"16002”、“部门”:“风湿病变态反应与免疫学”、“团体”:“”、“名称”、“Koh”}、{"project_code":"16003“、”dept“:”矫形外科“、”骨科“、”团体“:”“名称”:“project_code”},{"project_code":"16004",“部门”:“风湿病变态反应与免疫学”,“团体”:“”,“名称”:“林心荣”},{"project_code":"16005",“部门”:“物理疗法”,“团体”:“团体”:“名”:“李琨满”}

我的表名在PROJECT_CODE数据库中称为MySQL,其属性如下:

表名- PROJECT_code字段名: PROJECT_CODE、DEPARTMENT、REQUESTOR、

我希望将来自JSON的project_code、dept和name变量分别插入到MySQL表字段中。请帮帮忙。谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 02:50:38

代码语言:javascript
复制
$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);";
    */

}

此外,您还可以在单圈中插入多条记录--请检查下面的参考链接multiple.asp

在参考链接3中给出了不同的方法,因此,您可以使用任何适合您的方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50752280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档