将单独的sql语句中的值添加到json文件,或创建要更新的新JSON

时间:2017-08-31 12:51:25

标签: php mysql json

我目前正在运行一个执行sql select / insert的php脚本,并从select中创建一个数组,用于在脚本末尾的JSON文件中存储这些值。这按计划进行。

但是,我刚刚添加了一个新的sql语句来在插件完成后更新字段,我还需要找到一种方法将更新后的值插入到JSON文件中。我不知道怎么做,或者如果我可以在写入JSON的同一个while循环中执行它,我担心它需要新代码但我不知道如何写入或更新它JSON文件。

目前,写入该JSON文件的while循环将我的select语句$data中的每个元素作为$d并将其存储在数组中。 HOwever,我的新更新语句是$stmt2,我没有这个数组,因为该值在同一个表中受select / insert的影响。

以下是现有的选择/插入查询:

$data = mysqli_query($conn, " SELECT c.extension
                          , 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) and ANSWERED = 1) AS Total_Inbound
                          , sum(if(Answered = 1,0,1)) AS Total_Missed
                          , SUM(IF(LEGTYPE1 = 1, 1, 0)) +                   -- outbound
                            SUM(IF(LEGTYPE1 = 2, 1, 0) AND ANSWERED = 1) +  -- inbound
                            SUM(IF(Answered = 1, 0, 1))  AS Total_Calls
                          , NOW() AS Time_of_report
                          , curdate() AS Date_of_report
                      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 (7276,7314,7295,7306,7357,7200,7218,7247,7331,7255,7330,7000,7215,7240,7358,7312)
                      group by c.extension")  or die(mysqli_error( $conn));


$stmt = mysqli_prepare($conn2, "Insert into jfi.ambitionphone 
                    (Extension,Total_Talk_Time_seconds, 
                    Total_Talk_Time_minutes, Total_Outbound, Total_Inbound,
                    Missed_Calls, Total_Calls, Date_of_report,Time_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_Inbound = values(Total_Inbound),
                         Total_Outbound = values(Total_Outbound),
                         Missed_Calls = values(Missed_Calls),
                         Total_Calls = values(Total_Calls),
                         Time_of_report = NOW()
                         ") or die(mysqli_error( $conn2));

这是将所选数据写入JSON的while循环:

 $content = [];

 while ($d = mysqli_fetch_array($data, MYSQLI_ASSOC)) {

      mysqli_stmt_bind_param($stmt,"iidiiiiss", $d['extension'],
          $d['Total_Talk_Time_seconds'], $d['Total_Talk_Time_minutes'],
          $d['Total_Outbound'], $d['Total_Inbound'], $d['Total_Missed'], $d['Total_Calls'],
          $d['Time_of_report'], $d['Date_of_report']) ;
     mysqli_stmt_execute($stmt) or die(mysqli_error($conn2));

     // Store the current row
     $content[] = $d;

 }

 // Store it all
 file_put_contents('ambitionLog.json', json_encode($content, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT));

最后,这是新添加的更新声明:

$stmt2 = mysqli_prepare($conn2, "UPDATE jfi.ambitionphone a, 
   (SELECT * FROM jackson_id.users where id in 
   (48,75,76,77,81,82,83,85,86,87,99341,99344,99409,99422,99428)) j 
   SET a.ExtID = j.id where a.extension = j.extension") or die(mysqli_error( $conn2));

我需要做的就是以某种方式修改它,以便将$stmt2 ExtID中的值写入ambitionLog.json文件,同时将select语句中的其他元素写入while循环。其他所有内容保持不变,我只需要知道如何将此值放入JSON文件中。

0 个答案:

没有答案