嗨,伙计们,我正在尝试从php telnet cmd中读取一些数据,并解析它以更新mysql表中的特定结果。我现在面临的问题是,这是一个错误。
array(3) {
[0]=> string(5) "23000"
[1]=> int(1062)
[2]=> string(47) "Duplicate entry '1' for key 'UC_cdata_ont_info'"
} 未能执行查询
下面是我正在运行的cmd,“如果我只发送前两个cmds.. show_pon0和show_pon1,它从show_pon1 cmd.从0/0xxxxxxx开始的两个设备中读取数据.它在表mysql中更新它们.但是如果启用show_pon2 cmd命令,它会读取3种不同的设备数据作为下面的输出。
interface epon 0/0
OLT(config-interface-epon-0/0)# show ont info 1 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 1 1 AC:4E:91:A6:CF:F5 active online success match Test1-.
0/0 1 2 E0:67:B3:35:EF:76 active powerdown initial initial Test2.
-----------------------------------------------------------------------------
Total: 2, online 1
OLT(config-interface-epon-0/0)# show ont info 2 all
-----------------------------------------------------------------------------
F/S P ONT MAC Control Run Config Match Desc
ID flag state state state
----------------------------------------------------------------------------
0/0 2 1 E0:E8:E6:4E:62:58 active online success match Test3.
-----------------------------------------------------------------------------
Total: 1, online 1
OLT(config-interface-epon-0/0)#
OLT(config-interface-epon-0/0)# 下面的代码过滤以下数据
0/0 1 1 AC:4E:91:A6:CF:F5 active online success match Test1.
0/0 1 2 E0:67:B3:35:EF:76 active powerdown initial initial Test2.
0/0 2 1 E0:E8:E6:4E:62:58 active online success match Test3.它把它存储在mysql表上。
在代码下面
$show_pon[0] = "interface epon 0/0";
$show_pon[1] = "show ont info 1 all";
$show_pon[2] = "show ont info 2 all";
// $show_pon[2] = "show ont info 3 all";
// $show_pon[3] = "show ont info 4 all";
// $show_pon[4] = "show ont info 5 all";
// $show_pon[5] = "show ont info 6 all";
// $show_pon[6] = "show ont info 7 all";
// $show_pon[7] = "show ont info 8 all";
// $show_pon[8] = "show ont info 9 all";
// $show_pon[9] = "show ont info 10 all";
// $show_pon[10] = "show ont info 11 all";
// $show_pon[11] = "show ont info 12 all";
// $show_pon[12] = "show ont info 13 all";
// $show_pon[13] = "show ont info 14 all";
// $show_pon[14] = "show ont info 15 all";
// $show_pon[15] = "show ont info 16 all";
$debug = true;
if ($debug) {
$cache = '/tmp/cdata_ont_info.log';
$contents = @file_get_contents($cache);
if ($contents == false)
{
$telnet->DoCommand($show_pon, $poninfo_resposta);// estatisticas resposta.. coloca no array o dump resposta
file_put_contents($cache, $poninfo_resposta);
} else
{
$poninfo_resposta = $contents;
}
} else
{
$telnet->DoCommand($show_pon, $poninfo_resposta);// estatisticas resposta.. coloca no array o dump resposta
}
$filtro_resposta = $poninfo_resposta;
preg_match_all('~^\h*(\d+/\d+)\h+(\d+)\h+(\d+)\h+([A-F\d]{2}(?::[A-F\d]{2}){5})\h+(\S+)\h+(\S+)\h+(\S+)\h+(\S+)(?:\h+(\S+))?~m',$filtro_resposta,$m);
$InputArray = array($m[0]);
foreach ($InputArray as $str)
{
$query_chunks = [];
$query_data = [];
foreach( $str as $row )
{
$query_chunks[] = "(?,?,?,?,?,?,?,?,?,$id)";
$query_data = array_merge($query_data, preg_split("/\s+/", trim($row)));
}
$query = "INSERT INTO cdata_ont_info (frame_slot, pon, ont_id, mac_address, run_state, control_flag, match_state, config_state, description, olt_id) VALUES " . implode( ', ', $query_chunks );
$pdo_dbh = new PDO('mysql:host=localhost;dbname=testdb', 'mydbtestusr', 'mydbtestpasswd');
$sth = $pdo_dbh->prepare( $query );
if (!$sth) {
var_dump($sth->errorInfo());
die('ERRO!!! Falha na query!!!!');
}
if (!$sth->execute( $query_data ))
{
var_dump($sth->errorInfo());
die('ERRO!!! Falha executando a query_data!!!!!!');
}
die(print_r($query_data, true));
}就像我之前说的..。如果我只运行前两个cmds..。show_pon1和show_pon2.它通常更新mysql表中的数据。但如果我启用第3 cmd show_pon2或所有其他cmd继续..。它过滤来自query_date的所有数据,如下面的示例所示,但是它不会更新mysql表,它将抛出PDO错误1062重复的key1代码。
Array (
[0] => 0/0
[1] => 1
[2] => 1
[3] => AC:4E:91:A6:CF:F5
[4] => active
[5] => online
[6] => success
[7] => match
[8] => VILERMINA-.
[9] => 0/0
[10] => 1
[11] => 2
[12] => E0:67:B3:35:EF:76
[13] => active
[14] => powerdown
[15] => initial
[16] => initial
[17] => Rosiley-Si.
[18] => 0/0
[19] => 2
[20] => 1
[21] => E0:E8:E6:4E:62:58
[22] => active
[23] => online
[24] => success
[25] => match
[26] => CLOVES-NAV.
)并在下面的链接上附上mysql db的一些图片。


更新
以下是完全输出调试消息错误
> Warning: PDOStatement::execute(): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UC_cdata_ont_info' in /var/www/html/isp/olt/olt_cdata.php on line 263
SQL: [213] INSERT INTO cdata_ont_info (frame_slot, pon, ont_id, mac_address, run_state, control_flag, match_state, config_state, description, olt_id) VALUES (?,?,?,?,?,?,?,?,?,4), (?,?,?,?,?,?,?,?,?,4), (?,?,?,?,?,?,?,?,?,4) Sent SQL: [416] INSERT INTO cdata_ont_info (frame_slot, pon, ont_id, mac_address, run_state, control_flag, match_state, config_state, description, olt_id) VALUES ('0/0','1','1','AC:4E:91:A6:CF:F5','active','online','success','match','VILERMINA-.',4), ('0/0','1','2','E0:67:B3:35:EF:76','active','powerdown','initial','initial','Rosiley-Si.',4), ('0/0','2','1','E0:E8:E6:4E:62:58','active','online','success','match','CLOVES-NAV.',4) Params: 27 Key: Position #0: paramno=0 name=[0] "" is_param=1 param_type=2 Key: Position #1: paramno=1 name=[0] "" is_param=1 param_type=2 Key: Position #2: paramno=2 name=[0] "" is_param=1 param_type=2 Key: Position #3: paramno=3 name=[0] "" is_param=1 param_type=2 Key: Position #4: paramno=4 name=[0] "" is_param=1 param_type=2 Key: Position #5: paramno=5 name=[0] "" is_param=1 param_type=2 Key: Position #6: paramno=6 name=[0] "" is_param=1 param_type=2 Key: Position #7: paramno=7 name=[0] "" is_param=1 param_type=2 Key: Position #8: paramno=8 name=[0] "" is_param=1 param_type=2 Key: Position #9: paramno=9 name=[0] "" is_param=1 param_type=2 Key: Position #10: paramno=10 name=[0] "" is_param=1 param_type=2 Key: Position #11: paramno=11 name=[0] "" is_param=1 param_type=2 Key: Position #12: paramno=12 name=[0] "" is_param=1 param_type=2 Key: Position #13: paramno=13 name=[0] "" is_param=1 param_type=2 Key: Position #14: paramno=14 name=[0] "" is_param=1 param_type=2 Key: Position #15: paramno=15 name=[0] "" is_param=1 param_type=2 Key: Position #16: paramno=16 name=[0] "" is_param=1 param_type=2 Key: Position #17: paramno=17 name=[0] "" is_param=1 param_type=2 Key: Position #18: paramno=18 name=[0] "" is_param=1 param_type=2 Key: Position #19: paramno=19 name=[0] "" is_param=1 param_type=2 Key: Position #20: paramno=20 name=[0] "" is_param=1 param_type=2 Key: Position #21: paramno=21 name=[0] "" is_param=1 param_type=2 Key: Position #22: paramno=22 name=[0] "" is_param=1 param_type=2 Key: Position #23: paramno=23 name=[0] "" is_param=1 param_type=2 Key: Position #24: paramno=24 name=[0] "" is_param=1 param_type=2 Key: Position #25: paramno=25 name=[0] "" is_param=1 param_type=2 Key: Position #26: paramno=26 name=[0] "" is_param=1 param_type=2 NULL ERRO!!! Falha executando a query_data!!!!!!UPDATE2
下面的图片显示了创建表

发布于 2022-10-26 23:09:14
问题解决了,这是mysql表中列的一个问题,多亏了@TangentiallyPer交联帮助,我在mysql表上找到了错误,该表被定义为ont_id列上的唯一键,当我更改它时。在mysql表中更新了数据。
https://stackoverflow.com/questions/74202058
复制相似问题