Table of Contents
回复内容:
Home Backend Development PHP Tutorial mysql - php pdo 缺省错误模式 和 例外模式,差不多,为什么弄两个?

mysql - php pdo 缺省错误模式 和 例外模式,差不多,为什么弄两个?

Jun 06, 2016 pm 08:07 PM
mysql pdo php sql

先说 缺省模式

<code>$pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_SILENT);</code>
Copy after login
Copy after login

我做的实验中,出错后,会给 $pdo->errorCode() 和 $pdo->errorInfo() 赋值;通过调用他们可以可以 看 错误的详细信息;


然后说例外模式

<code>$pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);
try{

}catch(PDOException $e){
    echo "执行命令失败:".$e->getMessage();
    exit;
}</code>
Copy after login
Copy after login

会实例出一个 $e 对象,错误信息保存在这个属性里.

都是有错误信息,为什么要分两个属性?

回复内容:

先说 缺省模式

<code>$pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_SILENT);</code>
Copy after login
Copy after login

我做的实验中,出错后,会给 $pdo->errorCode() 和 $pdo->errorInfo() 赋值;通过调用他们可以可以 看 错误的详细信息;


然后说例外模式

<code>$pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);
try{

}catch(PDOException $e){
    echo "执行命令失败:".$e->getMessage();
    exit;
}</code>
Copy after login
Copy after login

会实例出一个 $e 对象,错误信息保存在这个属性里.

都是有错误信息,为什么要分两个属性?

/*错误模式说明:

<code>

◆异常模式:
    $pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_EXCEPTION);

    这个模式需要配合 try 使用 :
        一旦出错,就会:
        1.创建一个对象,$e 也可以随便命名,从 $e->getMessage() 获取错误信息;
        2.采取动作;

        try{
            
            要执行的命令...

        }catch(PDOException $e){
            echo "执行命令失败:".$e->getMessage();
            失败后的动作...
        }

    这个其实等于 ◆缺省模式的 if($pdo->errorInfo()[2]) ...

    创建PDO的时候,为什么也用 try?
        因为这时候连PDO对象都没有,所以也不能设置错误模式,所以可以用try,
        一旦PDO创建成功,错误模式就成了 ◆缺省模式,
        也就不能用try了.
</code>
Copy after login
<code>◆缺省模式:
    $pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_SILENT);

    这个出错,会给 $pdo->errorCode() 和 $pdo->errorInfo()[2] 赋值;

        可以用 
            if($pdo->errorInfo()[2]) 
            if($pdo->errorCode()) 
        判断脚本是不是出错.

    如果不出错,上面都是空的;
</code>
Copy after login
<code>◆警告模式:
    $pdo->setAttribute(PDO::ATTR_ERRMODE,  PDO::ERRMODE_WARNING);

    这个会echo出一个错误信息,打乱了脚本;
    学习调试的时候使用比较好;
    真用起来的时候,很多时候判断不出到底脚本有没有出错;
</code>
Copy after login

*/

你太纠结了...这是为了方便使用者,为啥一定要有个为什么。就像是有些API通过参数可以选择返回json还是xml,有什么特别的意义吗?也是为了方便而已。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

How does MySQL differ from Oracle? How does MySQL differ from Oracle? Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Getting Started with SQL: Essential Concepts and Skills Getting Started with SQL: Essential Concepts and Skills Apr 22, 2025 am 12:01 AM

SQL is a language used to manage and operate relational databases. 1. Create a table: Use CREATETABLE statements, such as CREATETABLEusers(idINTPRIMARYKEY, nameVARCHAR(100), emailVARCHAR(100)); 2. Insert, update, and delete data: Use INSERTINTO, UPDATE, DELETE statements, such as INSERTINTOusers(id, name, email)VALUES(1,'JohnDoe','john@example.com'); 3. Query data: Use SELECT statements, such as SELEC

See all articles