扩展 PDO 类:为什么该方法返回 0 而不是错误消息?

发布于 2024-10-19 18:52:02 字数 2113 浏览 5 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-10-26 18:52:02

因为 PDOStatement::execute 不会抛出,所以它仅在失败时返回 false。因此,您的代码永远不会进入 catch 块。应该更符合这样的思路:

$stmt = parent::prepare($query);

if (!$stmt->execute()) {
    self::get_error();
}

return $stmt->rowCount();

Because PDOStatement::execute does not throw, it only returns false upon failure. Hence your code never enters the catch block. Should be more along the lines of this:

$stmt = parent::prepare($query);

if (!$stmt->execute()) {
    self::get_error();
}

return $stmt->rowCount();
月光色 2024-10-26 18:52:02

当查询结果出错时,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文