且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理PHP中的MySQL外键错误?

更新时间:2022-05-13 23:26:19

使用try catch

use try catch

try { 
    $pdo->exec ("QUERY WITH SYNTAX ERROR"); 
} catch (PDOException $e) { 
    if ($e->getCode() == '23000') 
        echo "Syntax Error: ".$e->getMessage(); 
}

阅读 PDOStatement::errorCode

摘自返回代码列表

SQL-92标准定义了一组SQLSTATE返回代码. SQLSTATE 定义为五个字符的字符串,其中最左边的两个 字符定义错误类别,其余三个字符 定义错误子类.一些数据库供应商可能会扩展这些 返回码;以数字5到9开头的课程,以及 字母I到Z保留用于此类实现特定的 扩展名.特定JDBC操作的SQLSTATE代码可以是 通过SQLException

The SQL-92 standard defines a set of SQLSTATE return codes. SQLSTATE is defined as a five-character string, where the leftmost two characters define the error class, and the remaining three characters define the error subclass. Some database vendors may extend these return codes; classes beginning with the numbers 5 through 9 and letters I through Z are reserved for such implementation-specific extensions. The SQLSTATE code for a particular JDBC action can be retrieved via the getSQLState() method of SQLException