【问题标题】:How to Insert JSON into mysql table with PHP如何使用 PHP 将 JSON 插入 mysql 表
【发布时间】:2015-10-18 09:14:02
【问题描述】:

我已经尝试了很多网站来查找如何在 mysql 中插入 json,但我没有找到任何合适的解决方案。你能帮我在 mysql 表中插入波纹管 json 并告诉我表中列所需的名称吗?

    {
    "request" :  ` {
        "Target" : "Affiliate_Report",
        "Format" : "json",
        "Service" : "HasOffers",
        "Version" : "2",
        "NetworkId" : "network_id",
        "Method" : "getConversions",
        "api_key" : "api_key",
        "fields" : ["Stat.affiliate_info1", "Offer.name"],
        "limit" : "1"
    },
    "response" : {
        "status" : 1,
        "httpStatus" : 200,
        "data" : {
            "page" : 1,
            "current" : 1,
            "count" : 82,
            "pageCount" : 82,
            "data" : [{
                    "Stat" : {
                        "affiliate_info1" : "aashiq"
                    },
                    "Offer" : {
                        "name" : "App Download - Paytm - Android - IN - Incent"
                    }
                }
            ],
            "dbSource" : "branddb"
        },
        "errors" : [],
        "errorMessage" : null
    }
}

我需要从上面的 json 插入数据:aashiq, App Download - Paytm - Android - IN - Incent

【问题讨论】:

  • 给我们一些背景知识,例如您是否使用 MySQL 控制台进行操作,编程语言等。至于列名完全取决于您定义名称,我不相信有人可以帮助您
  • 你在研究 hasoffers API...我能问一下你什么时候开始使用 API...你想用 API 做什么。

标签: php mysql json


【解决方案1】:

我会试一试的。请耐心等待,因为我的 PHP 有点生疏了……所以假设 $mydata 是您存储 JSON 的数组对象。那么:

$data = $mydata["response"]["data"]; 
$name = $data[0]["Stat"]["affiliate_info1"]; // will store aashiq
$offer = $data[0]["Offer"]["name"]; // will store App Download etc...

使用名为 offers 的数据库创建一个 MySQL 数据库,其中包含一个表 offer 和三个字段:一个将自动递增的 IDaffiliate VARCHARfinal_offer VARCHAR

//copying from http://www.kodingmadesimple.com/2014/12/how-to-insert-json-data-into-mysql-php.html?m=1
//connect to mysql db
$con = mysql_connect("localhost","username","password") or die('Could not connect: ' . mysql_error());
//connect to the offers database
mysql_select_db("offers", $con);
//write your Insert query, you omit the autoincrement field and 
//after the keyword VALUES you use your variables enclosed in ''
$sql = "INSERT INTO offer(affiliate, final_offer) VALUES('$name', '$offer'"); 
if(!mysql_query($sql,$con))
{
    die('Error : ' . mysql_error());
}

我相信上面的代码或多或少是正确的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2018-04-20
    • 2019-01-08
    • 2014-03-13
    相关资源
    最近更新 更多