php - Undefined index error, multiple db connections in file -
i have php file/script running in powershell meant connect 1 db server, select info, connect db/server , insert info.
i having problems connections, have working, except when run script in powershell slew of errors, 5 exact (which matches records in database currently) undefined index.
this affects line 51 through 55 happen end of code, values
section, starting on duplicate key
line. table in mysql workbench has exact same column names, index values, etc. test table has execute script testing in workbench. calling values incorrectly in insert statement?
<?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) total_talk_time_seconds , round(sum(duration) / 60,2) total_talk_time_minutes , sum(if(legtype1 = 1,1,0)) total_outbound , sum(if(legtype1 = 2,1,0)) total_inbound , sum(if(answered = 1,0,1)) total_missed , count(distinct b.notablecallid) total_calls , now() time , curdate() date cdrdb.session left join cdrdb.callsummary b on a.notablecallid = b.notablecallid left join cdrdb.mxuser c on a.responsibleuserextensionid = c.extensionid b.ts >= curdate() , c.extension in (7295,7306,7218,7247,7330,7000,7358) group c.extension"); foreach ($data $d) { $stmt = mysqli_prepare($conn2, "insert 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:
this mysql query i'm converting php script, context:
insert 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) total_calls , sum(if(legtype1 = 1,1,0)) + sum(if(legtype1 = 2,1,0)) total_calls , now() , curdate() cdrdb.session left join cdrdb.callsummary b on a.notablecallid = b.notablecallid left join cdrdb.mxuser c on a.responsibleuserextensionid = c.extensionid b.ts >= curdate() , c.extension in (7295,7306,7218,7247,7330,7000,7358) group 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();
the problem didn't assign aliases function calls in first query. there's no columns named total_outbound
, total_inbound
, etc. should be:
$data = mysqli_query($conn, " select c.extension ,responsibleuserextensionid , sum(duration) total_talk_time_seconds , round(sum(duration) / 60,2) total_talk_time_minutes , sum(if(legtype1 = 1,1,0)) total_outbound , sum(if(legtype1 = 2,1,0)) total_inbound , sum(if(answered = 1,0,1)) total_missed , count(distinct b.notablecallid) total_calls , now() time , curdate() date cdrdb.session left join cdrdb.callsummary b on a.notablecallid = b.notablecallid left join cdrdb.mxuser c on a.responsibleuserextensionid = c.extensionid b.ts >= curdate() , c.extension in (7295,7306,7218,7247,7330,7000,7358) group c.extension");
then need fix syntax of insert
query. doesn't specify values insert, how supposed tell if you're creating duplicate key? , should use prepared statement avoid quoting issues.
$stmt = mysqli_prepare($conn2, "insert 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 $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); }
Comments
Post a Comment