来自PHP脚本的MySQL查询不插入新记录

时间:2017-08-18 13:01:55

标签: php mysql sql

我有一个PHP脚本,我是从一个MySQL查询构建的,它从一个数据库中读取并插入到另一个数据库中,并且应该在重复键上更新。

我前几天完成了这项工作,全天在powershell中运行它,它完成了初始插入并在整个过程中进行了适当的更新。我今天重新开始工作,在powershell中运行它,我在powershell中获得了成功的连接消息,没有错误,但是数据库表今天没有插入新记录。

为了进行比较,我在工作台上运行的关于表的测试版本的查询每天都会完美地插入新记录并在整个过程中进行更新。

没有错误,我无法确切地说明为什么这个脚本今天没有插入新记录。

我将发布下面的所有资源,但是有关详细信息,表结构完全相同(ID为AI主键,扩展名为Unique_of_report的唯一索引)

这是脚本:

    $servername = "";
    $username   = "";
    $password   = "";

    $servername2 = "";
    $username2   = "";
    $password2   = "*";

    // Create connection
    $conn  = new mysqli($servername, $username, $password);
    $conn2 = new mysqli($servername2, $username2, $password2);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";

    // Check connection2
    if ($conn2->connect_error) {
        die("Connection failed: " . $conn2->connect_error);
    }
    echo "Connected successfully";

    $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 ,
                                       sum(if(LEGTYPE1 = 1,1,0)) + sum(if(LEGTYPE1 = 2,1,0)) 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 (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, 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));



    // 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_of_report'], $d['Date_of_report']) or die(mysqli_error($conn2));
        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));

在工作台中完美插入/更新的原始查询:

INSERT INTO test.ambition_test (extension, extid, total_talk_time_seconds, total_talk_time_minutes, total_outbound, total_inbound, missed_calls, total_calls, time_of_report, date_of_report)
SELECT c.extension,
       responsibleuserextensionid,
       Sum(duration),
       Round(Sum(duration) / 60,2),
       Sum(IF(legtype1 = 1,1,0)),
       Sum(IF(legtype1 = 2,1,0)),
       Sum(IF(answered = 1,0,1)) -- , count(DISTINCT b.NOTABLECALLID) as Total_Calls
 ,
       Sum(IF(legtype1 = 1,1,0)) + Sum(IF(legtype1 = 2,1,0)) AS total_calls,
       Now(),
       Curdate()
FROM cdrdb.session a
LEFT JOIN cdrdb.callsummary b ON a.notablecallid = b.notablecallid
LEFT 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 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 = now();

0 个答案:

没有答案