未定义的索引错误,文件中的多个数据库连接

时间:2017-08-14 19:31:25

标签: php mysql sql indexing

我有一个在powershell中运行的php文件/脚本,用于连接到一个数据库服务器,选择信息,连接到另一个数据库/服务器并插入该信息。

我遇到了连接问题,但是现在我有了它的工作,除非我在powershell中运行这个脚本,我得到了一些错误,确切地说是5(这与当前数据库中的记录匹配)有关未定义的索引。

这会影响第51到55行,它恰好是我的代码VALUES部分的结尾,从ON DUPLICATE KEY行开始。我在mysql workbench中的表具有完全相同的列名,索引值等,我的测试表具有执行此脚本以在工作台中进行测试的位置。我在插入语句中是否错误地调用了这些值?

    <?php

    $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
                          , count(DISTINCT b.NOTABLECALLID) AS Total_Calls
                          , NOW() AS time
                          , curdate() AS date
                      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");


            foreach ($data as $d) {

                    $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)");
                            mysqli_stmt_bind_param($stmt, "ssiiiiiiss", $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']);

            mysqli_stmt_execute($stmt);

            }

     ?>

更新:

这是我正在转换为此PHP脚本的mysql查询,用于上下文:

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(); 

1 个答案:

答案 0 :(得分:1)

问题是您没有在第一个查询中为函数调用指定别名。因此,没有名为Total_OutboundTotal_Inbound等的列。应该是:

$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
                          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");

然后您还需要修复INSERT查询的语法。它没有指定要插入的值,那么它应该如何判断您是否正在创建重复键?你应该使用准备好的声明来避免引用问题。

$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)");

foreach ($data as $d) {
    mysqli_stmt_bind_param($stmt, "ssiiiiiiss", $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']);
    mysqli_stmt_execute($stmt);
}