简体   繁体   中英

Export sql results to JSON file

I'm trying to modify a php script to write to a JSON file when it is run every 15 minutes, and then overwrite the updated results. The script I have is doing this in the database (inserting and updating on duplicate) but my code for the JSON file is not working quite right.

When I manually export to JSON from workbench, I get this (which is what I 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 I run my actual PHP script below, I get 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 the results finally, but it's only one of 6 records, and it's the third in the table, so it doesn't really make sense to me why it would pull the third record as only one record. It's also not formatted in JSON by giving a new line to each value, but JSON is very foreign to me in general.

The PHP Script runs without errors and inserts into my database correctly, I just have to have this script write the results to a JSON file and overwrite when run every 15 minutes.

$data = mysqli_query($conn, " SELECT c.extension
                          ,RESPONSIBLEUSEREXTENSIONID
                          , sum(Duration) AS Total_Talk_Time_seconds
                          , round(sum(Duration) / 60,2) AS Total_Talk_Time_minutes
                          , sum(if(LEGTYPE1 = 1,1,0)) AS Total_Outbound
                          , sum(if(LEGTYPE1 = 2,1,0)) AS Total_Inbound
                          , sum(if(Answered = 1,0,1)) AS Total_Missed
                          , count(DISTINCT b.NOTABLECALLID) AS total_calls
                          , NOW() AS time
                          , curdate() AS date
                      FROM cdrdb.session a
                      INNER JOIN cdrdb.callsummary b
                           ON a.NOTABLECALLID = b.NOTABLECALLID
                      INNER join cdrdb.mxuser c
                           ON a.RESPONSIBLEUSEREXTENSIONID = c.EXTENSIONID
                      WHERE b.ts >= curdate()
                      AND c.extension IN (7295,7306,7218,7247,7330,7000,7358)
                      group by c.extension");


                      $stmt = mysqli_prepare($conn2, "Insert into 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 as $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 something I can do here to get the output like it is when I do the manual export?

You're currently overwriting the file on each iteration. Create a main array to store each iteration in:

// Create the 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 the current row in the main array
    $content[] = $d;
}

// Store the main array that contains all the rows
file_put_contents('ambitionLog.json', json_encode($content));

只需在输出区域中使用此行即可:echo json_encode $ your_final_array;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM