


Use warning mode-PDO::ERRMODE_WARNING (method 2 of capturing errors in SQL statements in PDO)
Use warning mode -PDO::ERRMODE_WARNING (Method 2 for capturing errors in SQL statements in PDO)
Warning mode will generate a PHP warning, and Set the errorCode attribute. If warning mode is set, the program will continue to run as it does unless the error code is explicitly checked.
Then in our previous article "Use the default mode-PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)", we introduced the first PDO method to capture errors in SQL statements, then in this article we will introduce the second method: using warning mode-PDO::ERRMODE_WARNING~!
Set the warning mode, read the data in the database through the prepare() and execute() methods, and complete the loop output of the data through the while statement and the fetch() method. The experience is executed after setting the warning mode. Wrong SQL statement.
Below I will use examples to further introduce the use of warning mode-PDO::ERRMODE_WARNING. First, let me introduce the idea of implementation.
First create a php file, connect to the MySQL database, execute the SELECT query operation through the prepare() and execute() methods of the prepared statement, and set an incorrect data table name, and set it to through the setAttribute() method. Warning mode, finally complete the loop output of data through while statement and fetch() method. The specific code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); //设置页面的编码格式 $dbms = "mysql"; // 数据库的类型 $dbName ="php_cn"; //使用的数据库名称 $user = "root"; //使用的数据库用户名 $pwd = "root"; //使用的数据库密码 $host = "localhost"; //使用的主机名称 $dsn = "$dbms:host=$host;dbname=$dbName"; try{ $pdo=new PDO($dsn,$user,$pwd);//初始化一个PDO对象,就是创建了数据库连接对象$pdo $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); //设置为警告模式 $query="select * from user_12";//需要执行的sql语句 $res=$pdo->prepare($query);//准备查询语句 $res->execute(); //执行查询语句,并返回结果集 ?> <table border="1" width="500"> <tr> <td height="22" align="center" valign="middle">id</td> <td height="22" align="center" valign="middle">用户名</td> <td height="22" align="center" valign="middle">密码</td> </tr> <?php while($result=$res->fetch(PDO::FETCH_ASSOC)){ // 循环输出查询结果集,并且设置结果集为关联数据形式。 ?> <tr> <td height="22" align="center" valign="middle"><?php echo $result["id"];?></td> <td height="22" align="center" valign="middle"><?php echo $result["username"];?></td> <td height="22" align="center" valign="middle"><?php echo $result["password"];?></td> </tr> <?php } }catch(Exception $e){ die("Error!:".$e->getMessage().'<br>'); } ?> </table>
Note:
In the above code, when defining the SELECT query statement, we deliberately used the wrong data table name user_12 (the correct data table name is: user). This is written for testing!
After setting to warning mode, if an error occurs in the SQL statement, a prompt message will be given, but the program can still continue to execute. The results obtained by the above example are as follows:
That’s all about the warning mode. You can try it locally. Then our next article will introduce you to the last SQL statement captured in PDO. For error methods, please read "Using Exception Mode-PDO::ERRMODE_EXCEPTION (Capturing Error Methods in SQL Statements in PDO Three)"!
The above is the detailed content of Use warning mode-PDO::ERRMODE_WARNING (method 2 of capturing errors in SQL statements in PDO). For more information, please follow other related articles on the PHP Chinese website!

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor
