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

爱⌒轻易说出口 提交于 2019-12-23 21:49:26

问题


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.

My php code:

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):

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

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


回答1:


(Upgrading to an answer)

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



回答2:


The stack trace shows the exception taking place in the fetch which is outside of the try/catch blocks.

The SELECT statement is triggering:

Integrity constraint violation: 1062 Duplicate entry 'theFieldContentOfTheLastRecordOfTheTable'

There's a unique key named theFieldContentOfTheLastRecordOfTheTable that it's conflicting with.

Can you post the schema for us to see how this could affect integrity?




回答3:


Look for dirty / duplicate data ALREADY in that column. Some apps allow a "loose" constraint to be applied AFTER data is already captured. This may have happen in your case.



来源:https://stackoverflow.com/questions/10430005/php-mysql-pdo-fatal-error-23000-eventhough-a-special-procedure-is-in-place

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!