如何在 PHP 中抽象 mysqli 准备好的语句?

发布于 2024-08-04 13:32:15 字数 925 浏览 3 评论 0原文

我正在使用自己的类进行数据库查询,扩展 mysqli:

class iDatabase extends mysqli
{
    public  $errorMsg;
    private $totalQueries;
    private $stmt;

    public function __construct()
    {
        parent::__construct( 'localhost', 'asd', 'asd', 'asd' );

        if ( mysqli_connect_errno() )
        {
            $this->errorMsg = 'Could not connect to server.<br /><i>' . mysqli_connect_error() . '</i>.';
            return;
        }

        parent::query( 'SET NAMES utf8' );
    }

}

但是,在执行查询和获取结果时我遇到了麻烦。我正在使用准备好的语句,但值和结果的绑定方式让我感到困惑。经过一番研究后,我想出了这个接受查询和参数的函数:

public function psQuery( $query, $params )
{
    $this->stmt = parent::prepare( $query );
    call_user_func_array( array($this->stmt,'bind_param'), $params );
    $this->stmt->execute();
}

我的问题是,从中获取结果的最佳方法是什么?我需要使用bind_result,然后获取每一行,然后关闭语句。我更愿意为每一行获取一个关联数组 - 这可能吗?

I'm using my own class for database queries, extending mysqli:

class iDatabase extends mysqli
{
    public  $errorMsg;
    private $totalQueries;
    private $stmt;

    public function __construct()
    {
        parent::__construct( 'localhost', 'asd', 'asd', 'asd' );

        if ( mysqli_connect_errno() )
        {
            $this->errorMsg = 'Could not connect to server.<br /><i>' . mysqli_connect_error() . '</i>.';
            return;
        }

        parent::query( 'SET NAMES utf8' );
    }

}

However I'm running into trouble when executing queries and getting back results. I'm using prepared statements, but the way values and results are bound is confusing me. After a bit of research I came up with this function that accepts the query and parameters:

public function psQuery( $query, $params )
{
    $this->stmt = parent::prepare( $query );
    call_user_func_array( array($this->stmt,'bind_param'), $params );
    $this->stmt->execute();
}

My question is, what is the best way to get results back from this? I need to use bind_result, then fetch each row, then close the statement. I'd prefer to just get an associative array for each row - is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

隐诗 2024-08-11 13:32:15

我在 Zend_Db_Adapter_MysqliZend_Db_Statement_Mysqli 类上做了很多工作来让它工作,因为我们想让它符合 PDO 和 <代码>PDOStatement接口。这是相当费力的,因为 MySQLi 坚持让您绑定变量来获取结果的方式令人困惑,而且 PDOStatement 支持多种获取模式。

如果你想看Zend_Db中的代码,请特别注意函数
Zend_Db_Statement_Mysqli::_execute()fetch()。基本上,_execute() 方法使用 call_user_func_array() 绑定变量引用数组。棘手的部分是您必须初始化数组,以便 bind_result() 函数获取引用。呃,这还不太清楚,所以去看一下代码。

或者只使用 PDO 的 MySQL 驱动程序。这就是我站在你的立场上会做的事情。

I worked on the Zend_Db_Adapter_Mysqli and Zend_Db_Statement_Mysqli classes quite a bit to get this to work, since we wanted to make it conform to the PDO and PDOStatement interface. It was pretty laborious, because of the confusing way MySQLi insists on making you bind variables to get results, and the variety of fetch modes supported by PDOStatement.

If you want to see the code in Zend_Db, pay special attention to the functions
Zend_Db_Statement_Mysqli::_execute() and fetch(). Basically, the _execute() method binds an array of variable references using call_user_func_array(). The tricky part is that you have to initialize the array so the bind_result() function gets the references. Uh, that wasn't totally clear, so go take a look at the code.

Or else just use PDO's MySQL driver. That's what I would do in your shoes.

我爱人 2024-08-11 13:32:15

我会看一下 Zend_Db 的实现,特别是mysqli 适配器,看看他们是如何做到的。

I would take a look at the implementation of Zend_Db, in particular the mysqli adapter, to see how they're doing it.

金兰素衣 2024-08-11 13:32:15

看来你必须按照你所说的“我需要使用bind_result,然后获取每一行,然后关闭语句”

我认为没有更简单的方法了。

It seems that you have to do what you said "I need to use bind_result, then fetch each row, then close the statement"

I think there is no simpler way.

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