我有一個運行在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();
你不必在查詢中用'while()'而不是'foreach()'獲取結果嗎? – Rasclatt
很確定你是對的@Rasclatt,因爲它不是PDO – GrumpyCrouton
,所以我會沿着'''的方向做一些事情,而($ row = mysql_fetch_array)'''?對不起,我不知道我將使用的確切語法 –