mysql - php pdo 缺省错误模式 和 例外模式,差不多,为什么弄两个?
先说 缺省模式
<code>$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);</code>
我做的实验中,出错后,会给 $pdo->errorCode() 和 $pdo->errorInfo() 赋值;通过调用他们可以可以 看 错误的详细信息;
然后说例外模式
<code>$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try{ }catch(PDOException $e){ echo "执行命令失败:".$e->getMessage(); exit; }</code>
会实例出一个 $e 对象,错误信息保存在这个属性里.
都是有错误信息,为什么要分两个属性?
回复内容:
先说 缺省模式
<code>$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);</code>
我做的实验中,出错后,会给 $pdo->errorCode() 和 $pdo->errorInfo() 赋值;通过调用他们可以可以 看 错误的详细信息;
然后说例外模式
<code>$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try{ }catch(PDOException $e){ echo "执行命令失败:".$e->getMessage(); exit; }</code>
会实例出一个 $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>
<code>◆缺省模式: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); 这个出错,会给 $pdo->errorCode() 和 $pdo->errorInfo()[2] 赋值; 可以用 if($pdo->errorInfo()[2]) if($pdo->errorCode()) 判断脚本是不是出错. 如果不出错,上面都是空的; </code>
<code>◆警告模式: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 这个会echo出一个错误信息,打乱了脚本; 学习调试的时候使用比较好; 真用起来的时候,很多时候判断不出到底脚本有没有出错; </code>
*/
你太纠结了...这是为了方便使用者,为啥一定要有个为什么。就像是有些API通过参数可以选择返回json还是xml,有什么特别的意义吗?也是为了方便而已。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.
