0

我们在 PHP PDO 中也有类似的方法吗???

我们isError()在 Pear mdb2 中有一个和工厂方法。

PEAR::isError($result)
MDB2::factory

我们在 PHP PDO 中也有类似的方法吗???

4

1 回答 1

0

Do we have a similar kind of methods in PHP PDO too?

Not quite, no.

Depending on how you've configured PDO error handling, you will either get false back from things that return errors, or you will get an exception. If you've turned exception handling off, you can check PDO::errorCode after you get a false when you would have expected something else. This can get very messy very quickly. Exceptions are best practice here.

PEAR's PEAR::isError() method came about in PHP4, when PHP did not have exceptions. It's an ugly workaround at best.

As for the factory, there's no need here. MDB2 was designed with a factory because each of the database types it can speak to is actually a different class. With PDO, you define the database type as part of the constructor and always get a PDO object back, not a subclass.

于 2012-03-12T19:38:10.977 回答