简体   繁体   中英

Why can't I catch the error with PDOException?

Get info passed by POST method, and trim all space in the string, then start a new pdo instance, connect mysql, and insert info passed by POST into table.

$title = trim($_POST["title"]);
$content = trim($_POST["content"]);

$dsn = "mysql:host=localhost;dbname=blog";
$con = new PDO($dsn,"root","xxxx");

$title = $con->quote($title);
$content = $con->quote($content);

try
{
    $sql = "insert into tmp (`title`,`content`) values('$title','$content')";
    $stmt = $con->prepare($sql);
    $stmt->execute();
}
catch(PDOException  $e)
{
    echo $e->getMessage();
}

The above is my PHP code to make the job done,the most import command is

insert into tmp (`title`,`content`) values('$title','$content')";

No error info is shown by running the above PHP code, and no error exists in /var/log/mysql/error.log , but info has not been inserted into the database.

I changed the

insert into tmp (`title`,`content`) values('$title','$content')";

into

insert into tmp (`title`,`content`) values($title,$content)";

The info passed by POST can be inserted into mysql now, the issue that confuses me is that:

  1. echo $e->getMessage(); take no effect at all.
  2. no error info in /var/log/mysql/error.log

How can I catch these errors?

The exception you are trying to catch will never be thrown, because you need to tell PDO how you want it to handle errors .

$con = new PDO($dsn,"root","xxxx");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Otherwise, the default PDO::ERRMODE_SILENT will be used:

This is the default mode. PDO will simply set the error code for you to inspect using the PDO::errorCode() and PDO::errorInfo() methods on both the statement and database objects; if the error resulted from a call on a statement object, you would invoke the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object. If the error resulted from a call on the database object, you would invoke those methods on the database object instead.

Tangentially, you should be using prepared statements . You are using a prepare() call, but you are not parametrizing the query and binding the variables as you should. Using quote() is not secure enough.


2020 Update:

Interestingly, starting with PHP 8, the default behaviour for PDO will change and will throw exceptions by default. The change was voted on this RFC , which mentions:

The current default error mode for PDO is silent. This means that when an SQL error occurs, no errors or warnings may be emitted and no exceptions thrown unless the developer implements their own explicit error handling.

This causes issues for new developers because the only errors they often see from PDO code are knock-on errors such as “call to fetch() on non-object” - there's no indication that the SQL query (or other action) failed or why.

When PHP 8 is released on November 2020, the default error mode will be PDO::ERRMODE_EXCEPTION .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM