[英]Issue with inserting data from multi-dimensional array in mySQL with PHP
PHP 的目的是遍历 API 调用返回的数组,并将结果行插入到 mySQL 表中。 该数组声明为 $data。
提供的示例数组有两个通话记录 [0] 和 [1],但它可以不返回通话记录,也可以返回许多通话记录,具体取决于通话量:
Array (
[call] => Array (
[0] => Array (
[hostname] => host.domain.net
[session_id] => 20221001155343061364
[orig_callid] => 0gip97admc1f6s4vbaok
[orig_match] => sip:user@domain.com
[orig_user] => 2001
[orig_domain] => domain.com
[orig_uri] => sip:tel@domain.com
[orig_name] => Peake
[ani] => tel
[dnis] => tel
[by_action] => Array ( )
[by_user] => Array ( )
[by_domain] => Array ( )
[by_callid] => Array ( )
[term_callid] => 20221001155343061365-f1ce8fd3cd1efdb3750535b87e7b7b79
[term_user] => Array ( )
[term_domain] => *
[term_uri] => sip:12028410090@host
[time_begin] => 2022-10-01 15:53:43
[time_answer] => 0000-00-00 00:00:00
[orig_call_info] => progressing
[term_call_info] => alerting
[media] => G.711 u-law
)
[1] => Array (
[hostname] => host.domain.net
[session_id] => 20221001155348027249
[orig_callid] => 2311si08u8u8m5udenak
[orig_match] => sip:user@domain.com
[orig_user] => 2001
[orig_domain] => domain.com
[orig_uri] => sip:tel@domain.com
[orig_name] => Peake
[ani] => tel
[dnis] => tel
[by_action] => Array ( )
[by_user] => Array ( )
[by_domain] => Array ( )
[by_callid] => Array ( )
[term_callid] => 20221001155348027250-f1ce8fd3cd1efdb3750535b87e7b7b79
[term_user] => Array ( )
[term_domain] => *
[term_uri] => sip:tel@host
[time_begin] => 2022-10-01 15:53:48
[time_answer] => 2022-10-01 15:53:50
[orig_call_info] => active
[term_call_info] => active
[media] => PCMU
)
)
)
这是我的 PHP:
<?php
foreach($data as $i => $item) {
$link = mysqli_connect("127.0.0.1", "db", "password", "table");
$sql = "INSERT INTO ns_cdr (session_id,orig_user, dnis, ani, time_begin)
VALUES ('".$data[$i]['session_id']."',
'".$data[$i]['orig_match']."',
'".$data[$i]['dnis']."',
'".$data[$i]['ani']."',
'".$data[$i]['time_begin']."')
ON DUPLICATE KEY UPDATE
orig_user = '".$data[$i]['orig_match']."',
dnis = '".$data[$i]['dnis']."',
ani = '".$data[$i]['ani']."',
time_begin='".$data[$i]['time_begin']."'
";
if(mysqli_query($link, $sql)){
echo "";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
//}
}
?>
这是结果:
Notice: Undefined index: session_id in /var/www/html/tool_getcalls.php on line 50
Notice: Undefined index: orig_match in /var/www/html/tool_getcalls.php on line 51
Notice: Undefined index: dnis in /var/www/html/tool_getcalls.php on line 52
Notice: Undefined index: ani in /var/www/html/tool_getcalls.php on line 53
Notice: Undefined index: time_begin in /var/www/html/tool_getcalls.php on line 54
Notice: Undefined index: orig_match in /var/www/html/tool_getcalls.php on line 56
Notice: Undefined index: dnis in /var/www/html/tool_getcalls.php on line 57
Notice: Undefined index: ani in /var/www/html/tool_getcalls.php on line 58
Notice: Undefined index: time_begin in /var/www/html/tool_getcalls.php on line 59
只是不确定如何正确处理循环以解析数组内容。 任何帮助表示赞赏!
--------
$SRCDATA
--------
Array (
[call] => Array (
[0] => Array (
[hostname] => host.domain.net
[session_id] => 20221001155343061364
[orig_callid] => 0gip97admc1f6s4vbaok
[orig_match] => sip:user@domain.com
[orig_user] => 2001
[orig_domain] => domain.com
[orig_uri] => sip:tel@domain.com
[orig_name] => Peake
[ani] => tel
[dnis] => tel
[by_action] => Array ( )
[by_user] => Array ( )
[by_domain] => Array ( )
[by_callid] => Array ( )
[term_callid] => 20221001155343061365-f1ce8fd3cd1efdb3750535b87e7b7b79
[term_user] => Array ( )
[term_domain] => *
[term_uri] => sip:12028410090@host
[time_begin] => 2022-10-01 15:53:43
[time_answer] => 0000-00-00 00:00:00
[orig_call_info] => progressing
[term_call_info] => alerting
[media] => G.711 u-law
)
[1] => Array (
[hostname] => host.domain.net
[session_id] => 20221001155348027249
[orig_callid] => 2311si08u8u8m5udenak
[orig_match] => sip:user@domain.com
[orig_user] => 2001
[orig_domain] => domain.com
[orig_uri] => sip:tel@domain.com
[orig_name] => Peake
[ani] => tel
[dnis] => tel
[by_action] => Array ( )
[by_user] => Array ( )
[by_domain] => Array ( )
[by_callid] => Array ( )
[term_callid] => 20221001155348027250-f1ce8fd3cd1efdb3750535b87e7b7b79
[term_user] => Array ( )
[term_domain] => *
[term_uri] => sip:tel@host
[time_begin] => 2022-10-01 15:53:48
[time_answer] => 2022-10-01 15:53:50
[orig_call_info] => active
[term_call_info] => active
[media] => PCMU
)
)
)
并使用准备好的语句处理 api 响应
/* craft the sql with placeholders */
$sql='insert into `ns_cdr` ( `session_id`, `orig_user`, `dnis`, `ani`, `time_begin` )
values ( ?, ?, ?, ?, ? )
on duplicate key update
`orig_user`=?,
`dnis`=?,
`ani`=?,
`time_begin`=?';
/*
create the prepared statement and bind the placeholders to variables.
These variables do not need to exist at this stage when using mysqli
but with PDO this is not the case.
*/
$stmt=$link->prepare( $sql );
$stmt->bind_param( 'sssssssss', $sid, $usr, $dnis, $ani, $begin, $usr, $dnis, $ani, $begin );
/*
$SRCDATA represents the response from the api. The top-level of this
array is keyed under `call` so you need to process under that key.
*/
$data=$SRCDATA['call'];
/*
loop through the data array and, for simplicity, cast the
child arrays as objects to allow use of arrow notation.
*/
foreach( $data as $index => $item ){
$obj=(object)$item;
$sid=$obj->session_id;
$usr=$obj->orig_match;
$dnis=$obj->dnis;
$ani=$obj->ani;
$begin=$obj->time_begin;
# commit the data to the db
$stmt->execute();
}
$stmt->close();
$link->close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.