php - Export sql results to JSON file -
i'm trying modify php script write json file when run every 15 minutes, , overwrite updated results. script have doing in database (inserting , updating on duplicate) code json file not working quite right.
when manually export json workbench, (which want):
[ { "id" : 1, "extension" : 7218, "extid" : 35302, "total_talk_time_seconds" : 11, "total_talk_time_minutes" : 0.18, "total_inbound" : 0, "total_outbound" : 1, "missed_calls" : 0, "total_calls" : null, "date_of_report" : "2017-08-15", "time_of_report" : "2017-08-15 08:58:31" }, { "id" : 2, "extension" : 7306, "extid" : 35370, "total_talk_time_seconds" : 762, "total_talk_time_minutes" : 12.7, "total_inbound" : 4, "total_outbound" : 0, "missed_calls" : 3, "total_calls" : null, "date_of_report" : "2017-08-15", "time_of_report" : "2017-08-15 08:58:31" }, { "id" : 3, "extension" : 7358, "extid" : 35278, "total_talk_time_seconds" : 41, "total_talk_time_minutes" : 0.68, "total_inbound" : 0, "total_outbound" : 0, "missed_calls" : 0, "total_calls" : null, "date_of_report" : "2017-08-15", "time_of_report" : "2017-08-15 08:58:31" } ] but when run actual php script below, this:
{"extension":"7358","responsibleuserextensionid":"35278","total_talk_time_seconds":"41","total_talk_time_minutes":"0.68","total_outbound":"0","total_inbound":"0","total_missed":"0","total_calls":"0","time":"2017-08-15 10:05:07","date":"2017-08-15"} so, it's writing results finally, it's 1 of 6 records, , it's third in table, doesn't make sense me why pull third record 1 record. it's not formatted in json giving new line each value, json foreign me in general.
the php script runs without errors , inserts database correctly, have have script write results json file , overwrite when run every 15 minutes.
$data = mysqli_query($conn, " select c.extension ,responsibleuserextensionid , sum(duration) total_talk_time_seconds , round(sum(duration) / 60,2) total_talk_time_minutes , sum(if(legtype1 = 1,1,0)) total_outbound , sum(if(legtype1 = 2,1,0)) total_inbound , sum(if(answered = 1,0,1)) total_missed , count(distinct b.notablecallid) total_calls , now() time , curdate() date cdrdb.session inner join cdrdb.callsummary b on a.notablecallid = b.notablecallid inner join cdrdb.mxuser c on a.responsibleuserextensionid = c.extensionid b.ts >= curdate() , c.extension in (7295,7306,7218,7247,7330,7000,7358) group c.extension"); $stmt = mysqli_prepare($conn2, "insert jfi.ambitionphone(extension, extid, total_talk_time_seconds, total_talk_time_minutes,total_outbound, total_inbound, missed_calls, total_calls, time_of_report,date_of_report ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) on duplicate key update total_talk_time_seconds = values(total_talk_time_seconds), total_talk_time_minutes = values(total_talk_time_minutes), total_outbound = values(total_outbound), total_inbound = values(total_inbound), missed_calls = values(missed_calls), total_calls = values(total_calls), time_of_report = values(time_of_report), date_of_report = values(date_of_report)") or die(mysqli_error($conn2)); foreach ($data $d) { mysqli_stmt_bind_param($stmt,"iiidiiiiss", $d['extension'], $d['responsibleuserextensionid'], $d['total_talk_time_seconds'], $d['total_talk_time_minutes'], $d['total_outbound'], $d['total_inbound'], $d['total_missed'], $d['total_calls'], $d['time'], $d['date']) or die(mysqli_error($conn2)); mysqli_stmt_execute($stmt) or die(mysqli_error($conn2)); $ambitionjsondata = json_encode($d); file_put_contents('ambitionlog.json', $ambitionjsondata); } is there can here output when manual export?
you're overwriting file on each iteration. create main array store each iteration in:
// create main array $content = []; while ($d = mysqli_fetch_array($data, mysqli_assoc)) { mysqli_stmt_bind_param($stmt,"iiidiiiiss", $d['extension'], $d['responsibleuserextensionid'], $d['total_talk_time_seconds'], $d['total_talk_time_minutes'], $d['total_outbound'], $d['total_inbound'], $d['total_missed'], $d['total_calls'], $d['time'], $d['date']) or die(mysqli_error($conn2)); mysqli_stmt_execute($stmt) or die(mysqli_error($conn2)); // store current row in main array $content[] = $d; } // store main array contains rows file_put_contents('ambitionlog.json', json_encode($content));
Comments
Post a Comment