2017-08-14 50 views
0

我有一個運行在powershell中的php文件/腳本,它意味着連接到一個數據庫服務器,選擇信息,連接到另一個數據庫/服務器並插入該信息。未定義的索引錯誤,文件中的多個數據庫連接

我有連接問題,但現在我有它的工作,除非我在PowerShell中運行此腳本我得到了一個錯誤,5是確切的(它與數據庫中的記錄匹配當前)關於undefined指數。

這會影響第51行到第55行,它們恰好是我的代碼的末尾,VALUES部分,從ON DUPLICATE KEY行開始。我在mysql工作臺中的表具有完全相同的列名,索引值等,我的測試表具有執行此腳本以在工作臺中進行測試的位置。我是否在我的插入語句中錯誤地調用了這些值?

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

      } 

    ?> 

UPDATE:

這是MySQL查詢,我將這個PHP腳本,用於上下文:

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

你不必在查詢中用'while()'而不是'foreach()'獲取結果嗎? – Rasclatt

+1

很確定你是對的@Rasclatt,因爲它不是PDO – GrumpyCrouton

+0

,所以我會沿着'''的方向做一些事情,而($ row = mysql_fetch_array)'''?對不起,我不知道我將使用的確切語法 –

回答

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); 
} 
+0

謝謝,$ stmt塊,所有這些都在我的foreach循環中,是否正確? –

+0

實際上,你只需要在循環之外調用'prepare'和'bind_param'一次。 – Barmar

+0

對不起,我只是有點困惑,我認爲選擇查詢必須在循環之外完成以創建$ data變量。我更新了上面的代碼,但我不確定要從foreach循環中刪除什麼 –