Simulate a PDO fetch failure situation

前端 未结 3 1188
無奈伤痛
無奈伤痛 2021-01-20 05:27

Conform to the php docs, the PDO method fetch() returns the value FALSE both when no records are found AND on failure (e.g. when something goes

3条回答
  •  隐瞒了意图╮
    2021-01-20 05:53

    With PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION fetch will always throw an exception if there is an error. You can handle those in your catch block and it doesn't matter what it returned. Therefore, if you didn't catch an exception and it returned false you can safely assume that it was due to an empty set. This is a very effective way have handling PDO errors. To answer your question, there are many ways of simulating an error. The most basic is improper query syntax. You can also try to bind a parameter that isn't there, bind the wrong number of parameters etc. The longer you use this scheme the more types of errors/exceptions you will see. It is very effective since each error includes a detailed message to help you debug it.

提交回复
热议问题