简体   繁体   English

PHP MYSQL PDO - >致命错误23000即使有特殊程序也是如此

[英]PHP MYSQL PDO -> Fatal Error 23000 eventhough a special procedure is in place

I have a table in my db. 我的数据库中有一张表。 My table has several fields including an auto-incremented id field set as primary key, and one other field called 'reference' that I did set as unique. 我的表有几个字段,包括一个自动递增的id字段设置为主键,另一个字段称为'reference',我设置为唯一。 To populate that table, I have a php script that insert records in that table using pdo. 为了填充该表,我有一个php脚本,使用pdo在该表中插入记录。 Every time an insert has been made successfully (meaning the 'reference' did not exist in the table), I increment a variable called $newOnes. 每次成功插入(意味着表中不存在'reference')时,我会增加一个名为$ newOnes的变量。 If the value 'reference' is already in the table, an exception with the code 23000 is triggered. 如果值'reference'已在表中,则会触发代码为23000的异常。 In that case, I increment another variable called $doublons. 在这种情况下,我增加另一个名为$ doublons的变量。 Unfortunately my script is triggering a fatal error with exception 23000 when the while loop is "handling" the last record of the table . 不幸的是, 当while循环“处理”表的最后一条记录时,我的脚本会触发异常23000的致命错误。 And I do not get it. 我不明白。 Thank you in advance for your help. 预先感谢您的帮助。 Cheers. 干杯。 Marc. 渣。

My php code: 我的PHP代码:

try {
  $connexion = connexion('localhost', 'user', 'user', 'mydb');
  $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $qry_bat = $connexion->query('SELECT...');
  $ins_db = $connexion->prepare('INSERT...');
}
catch (PDOException $e) {
  echo $e->getMessage();
}

while($row = $qry_bat->fetch(PDO::FETCH_ASSOC)) {
  try {
    $ins_db->execute(array(...));
    $newOnes++;
  }
  catch (PDOException $e) {
    if ($e->getCode() != 23000) {
      echo '<span class="msg-alert">'.$e->getMessage().'</span>';
    } else {
      $doublons++;
    }
  }
}

The fatal error I am getting (note that line 22 refers to the while(...) line): 我得到的致命错误(注意第22行是指while(...)行):

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]:  
Integrity constraint violation: 1062 Duplicate entry 'theFieldContentOfTheLastRecordOfTheTable' for key 'theFieldNameReference' in  
/myFilePath/file.php:22 Stack trace: #0 /myFilePath/file.php(22): PDOStatement->fetch(2)  
#1 {main} thrown in /myFilePath/file.php on line 22

EDIT ////////// 编辑//////////

original table (things to mentionne): 原始表(提到的东西):
auto incremented id 自动递增的ID

table to insert in (things to mentionne): 要插入的表(提到的东西):
auto incremented on id field 在id字段上自动递增
UNIQUE INDEX on reference UNIQUE INDEX参考

( Upgrading to an answer ) 升级到答案

Looks like this bug , which is still open after almost five years; 看起来像这个bug ,差不多五年后仍然开放; try instead: 试着改为:

while (true) {
  try {
    $row = $qry_bat->fetch(PDO::FETCH_ASSOC);
    if (!$row) break;
    $ins_db->execute(array(...));
    $newOnes++;
  }
  catch (PDOException $e) {
    if ($e->getCode() != 23000) {
      echo '<span class="msg-alert">'.$e->getMessage().'</span>';
    } else {
      $doublons++;
    }
  }
}

The stack trace shows the exception taking place in the fetch which is outside of the try/catch blocks. 堆栈跟踪显示fetch中发生的异常,该异常位于try / catch块之外。

The SELECT statement is triggering: SELECT语句正在触发:

Integrity constraint violation: 1062 Duplicate entry 'theFieldContentOfTheLastRecordOfTheTable'

There's a unique key named theFieldContentOfTheLastRecordOfTheTable that it's conflicting with. 有一个名为theFieldContentOfTheLastRecordOfTheTable的唯一键,它与之相冲突。

Can you post the schema for us to see how this could affect integrity? 您可以为我们发布架构,看看这会如何影响完整性?

Look for dirty / duplicate data ALREADY in that column. 在该列中查找脏/重复数据ALREADY。 Some apps allow a "loose" constraint to be applied AFTER data is already captured. 某些应用程序允许在已捕获数据后应用“松散”约束。 This may have happen in your case. 这可能发生在你的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM