How to handle MySQL's Foreign Key Error in Php?

前端 未结 1 662
暖寄归人
暖寄归人 2021-01-19 03:04

I was testing foreign keys in MySQL. The objective is to prevent the entry of an id into table2, that does not exist in table1. I was

相关标签:
1条回答
  • 2021-01-19 03:47

    use try catch

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

    Read PDOStatement::errorCode

    taken from Return Code list

    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

    0 讨论(0)
提交回复
热议问题