扩展 PDO 类:为什么该方法返回 0 而不是错误消息?
跟进这里的帖子,似乎我已经成功扩展了pdo类,
class database_extended extends PDO
{
#make a connection
public function __construct($dsn,$username,$password)
{
try
{
parent::__construct($dsn,$username,$password);
//$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
# call the get_error function
self::get_error($e);
}
}
#get the number of rows in a result
public function num_rows($query)
{
try
{
# create a prepared statement
$stmt = parent::prepare($query);
# execute query
$stmt->execute();
# return the result
return $stmt->rowCount();
}
catch (PDOException $e)
{
# call the get_error function
self::get_error($e);
}
}
# display error
public function get_error($e)
{
$this->connection = null;
die($e->getMessage());
}
# closes the database connection when object is destroyed.
public function __destruct()
{
}
}
但似乎不太正确 - 我故意在查询中测试了 num_rows
方法,因此该方法可以返回错误消息,
# the host used to access DB
define('DB_HOST', 'localhost');
# the username used to access DB
define('DB_USER', 'root');
# the password for the username
define('DB_PASS', 'xx');
# the name of your databse
define('DB_NAME', 'xx_2011');
# the data source name
define('DSN', 'mysql:dbname='.DB_NAME.';host='.DB_HOST);
include 'class_database.php';
$connection = new database_extended(DSN,DB_USER,DB_PASS);
$sql = "
SELECT *
FROM table_not_exist
ORDER BY cnt_id DESC
";
echo $connection->num_rows($sql);
它应该返回,
SQLSTATE[42S02]:基表或视图 未找到:1146 个表 “xx_2011.table_not_exist”不存在 存在
但它返回一个0
!为什么??我该如何修复它?
谢谢。
Following up on the post here, it seems that I have managed to extend the pdo class,
class database_extended extends PDO
{
#make a connection
public function __construct($dsn,$username,$password)
{
try
{
parent::__construct($dsn,$username,$password);
//$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
# call the get_error function
self::get_error($e);
}
}
#get the number of rows in a result
public function num_rows($query)
{
try
{
# create a prepared statement
$stmt = parent::prepare($query);
# execute query
$stmt->execute();
# return the result
return $stmt->rowCount();
}
catch (PDOException $e)
{
# call the get_error function
self::get_error($e);
}
}
# display error
public function get_error($e)
{
$this->connection = null;
die($e->getMessage());
}
# closes the database connection when object is destroyed.
public function __destruct()
{
}
}
But it seems not quite right - I tested the num_rows
method with a mistake in the query on purpose, so this method can return an error message,
# the host used to access DB
define('DB_HOST', 'localhost');
# the username used to access DB
define('DB_USER', 'root');
# the password for the username
define('DB_PASS', 'xx');
# the name of your databse
define('DB_NAME', 'xx_2011');
# the data source name
define('DSN', 'mysql:dbname='.DB_NAME.';host='.DB_HOST);
include 'class_database.php';
$connection = new database_extended(DSN,DB_USER,DB_PASS);
$sql = "
SELECT *
FROM table_not_exist
ORDER BY cnt_id DESC
";
echo $connection->num_rows($sql);
It should returns,
SQLSTATE[42S02]: Base table or view
not found: 1146 Table
'xx_2011.table_not_exist' doesn't
exist
But it returns a 0
instead! Why?? How can I fix it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为
PDOStatement::execute
不会抛出
,所以它仅在失败时返回false
。因此,您的代码永远不会进入catch
块。应该更符合这样的思路:Because
PDOStatement::execute
does notthrow
, it only returnsfalse
upon failure. Hence your code never enters thecatch
block. Should be more along the lines of this:当查询结果出错时,
PDOStatement::execute
返回 false,但不会引发异常。如果您想对错误执行某些操作,您可能应该编写一些代码来检查该返回值。PDOStatement::execute
returns false when the query results in an error, it doesn't throw an exception. You should probably write some code to check that return value if you want to do something with the error.