I've been trying to slowly migrate a script that selects data, and inserts based on bound parameters into another table from mysql to db2.
I have most of it migrated, but this main portion is still failing to insert. My select is working, returning exactly what I expect. However, something is going wrong in the bit where I create the array or parameter values. I"m simply trying to iterate through the selected value rows and insert the values into a matching table.
I'm I using odbc_fetch_array incorrectly, or does it look like something's wrong with my bound parameters?
//Main query to select data
$data = "
SELECT
u.extension
, sum(duration) as total_talk_time_seconds
, round(sum(duration) / 60,2) as total_talk_time_minutes
, sum(case when legtype1 = 1 then 1 else 0 end) as total_outbound
from SESSION a
join call_summary b
on a.notablecallid = b.notablecallid
inner join system_USERS u
on u.EXTENSION = CALLINGPARTYNO or u.EXTENSION = FINALLYCALLEDPARTYNO
group by extension,u.user_id" or die(db2_conn_error($DB2Conn));
$stmt = "
INSERT into daily_call_totals
(extension,
total_talk_time_seconds,
total_talk_time_minutes,
total_outbound)
VALUES (?, ?, ?, ?)" or die(db2_conn_error($DB2Conn));
//create array for binding
$content = [];
$mainResult = odbc_exec($DB2Conn, $data);
while ($d = odbc_fetch_array($mainResult)) {
$prepInsert = odbc_prepare($DB2Conn, $stmt);
//for each row, bind param. This is to ensure we get the correct number of records whether they're being inserted or updated for duplicates
$values = [
$d['extension'],
$d['total_talk_time_seconds'],
$d['total_talk_time_minutes'],
$d['total_outbound']];
// Store the current row
$content[] = $d;
if($prepInsert){
$result = odbc_execute($prepInsert,$values);
if($result){
print "successfully added record";
}
}
}
INSERT <data> FROM SELECT, which is faster and would involve no bound parameters. If you need to know which rows were inserted, you want theFINAL TABLEfeature. If you need to perform some filtering first, though, obviously this won't work. Otherwise, we need to have a better idea of what the actual problem is. Are you getting an error? No results? Are you even getting rows to add?MERGEwould be the answer.