PHP MYSQL PDO->致命错误23000即使已执行特殊程序 [英] PHP MYSQL PDO -> Fatal Error 23000 eventhough a special procedure is in place

查看:79
本文介绍了PHP MYSQL PDO->致命错误23000即使已执行特殊程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个表.我的表有几个字段,包括一个设置为主键的自动递增的id字段,以及另一个我设置为唯一的称为引用"的字段.要填充该表,我有一个php脚本,可使用pdo在该表中插入记录.每次成功完成插入操作(意味着表中不存在引用")时,我都会递增一个名为$ newOnes的变量.如果表中已经存在引用"值,则会触发代码23000的异常.在这种情况下,我增加了另一个名为$ doublons的变量.不幸的是,当while循环正在处理"表的最后一条记录时,我的脚本触发了致命错误,异常23000 .而且我不明白.预先感谢您的帮助.干杯.马克.

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. To populate that table, I have a php script that insert records in that table using pdo. Every time an insert has been made successfully (meaning the 'reference' did not exist in the table), I increment a variable called $newOnes. If the value 'reference' is already in the table, an exception with the code 23000 is triggered. In that case, I increment another variable called $doublons. Unfortunately my script is triggering a fatal error with exception 23000 when the while loop is "handling" the last record of the table. And I do not get it. Thank you in advance for your help. Cheers. Marc.

我的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++;
    }
  }
}

我遇到的致命错误(请注意,第22行指的是while(...)行):

The fatal error I am getting (note that line 22 refers to the while(...) line):

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 //////////

原始表(要提到的东西):
自动递增的ID

original table (things to mentionne):
auto incremented id

要插入的表格(要提及的内容):
在ID字段上自动递增
参考上的唯一索引

table to insert in (things to mentionne):
auto incremented on id field
UNIQUE INDEX on reference

推荐答案

(升级为答案)

看起来像此错误,该错误在将近五年后仍然开放;改为尝试:

Looks like this bug, which is still open after almost five years; 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++;
    }
  }
}

这篇关于PHP MYSQL PDO-&gt;致命错误23000即使已执行特殊程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆