doupingmao1903 2015-07-08 11:31
浏览 26

使用PHP将Json数据插入SQL Server 2012

I am parsing a data from a JSON link then I would like to insert it into one table in SQL Server 2012. The parsing are working well, My only problem is the inserting of data into SQL Server.

Sample JSON Data:

"results": [   {
   "name": "The JPT Look On Do More - Commercial Transformation",
   "metrics": {
    "video": {
     "playthrough_75": "2981",
     "plays": "5466",
     "time_watched": "4030502288",
     "playthrough_100": "2288",
     "uniq_plays": {
      "monthly_uniqs": "4731",
      "daily_uniqs": "5136",
      "weekly_uniqs": "4991"
     },

Script:

$context = stream_context_create($opts);
$content = file_get_contents($url, false, $context);
$json = json_decode($content, true);

foreach($json['results'] as $item)
{
$VideoName = $item['name'];
$playthrough_75 = $item['metrics']['video']['playthrough_75'];
$plays = $item['metrics']['video']['plays'];
$time_watched = $item['metrics']['video']['time_watched'];
$playthrough_100 = $item['metrics']['video']['playthrough_100'];
$plays_monthly_uniqs = $item['metrics']['video']['uniq_plays']['monthly_uniqs'];
$plays_daily_uniqs = $item['metrics']['video']['uniq_plays']['daily_uniqs'];
$plays_weekly_uniqs = $item['metrics']['video']['uniq_plays']['weekly_uniqs'];

$serverName = "SERVER_NAME";
$connectionInfo = array("Database"=>"Web_Analytics");
$conn = sqlsrv_connect($serverName, $connectionInfo);

*INSERT SQL SCRIPT HERE* 
}

So from the script above, I stored each field into a variable and then What I would like to do is to execute this query below. How could I execute it using PHP?

$sql = "INSERT INTO Ooyala_Analytics([VideoName]
      ,[VideoId]
      ,[playthrough_75]
      ,[plays]
      ,[time_watched]
      ,[playthrough_100]
      ,[plays_monthly_uniqs]
      ,[plays_daily_uniqs]
      ,[plays_weekly_uniqs]
      ,)
      VALUES('$VideoName'
        ,'$VideoId'
        ,'$playthrough_75'
        ,'$plays'
        ,'$time_watched'
        ,'$playthrough_100'
        ,'$plays_monthly_uniqs'
        ,'$plays_daily_uniqs'
        ,'$plays_weekly_uniqs' 
        ')";
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分