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'
')";
}