2

我正在使用自己的类进行数据库查询,扩展 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,然后获取每一行,然后关闭语句。我宁愿只为每一行获取一个关联数组——这可能吗?

4

3 回答 3

4

我在Zend_Db_Adapter_MysqliandZend_Db_Statement_Mysqli类上做了很多工作来让它工作,因为我们想让它符合PDOandPDOStatement接口。这是相当费力的,因为 MySQLi 坚持让你绑定变量以获得结果的令人困惑的方式,以及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.

于 2009-09-07T02:37:23.480 回答
1

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

于 2009-09-07T01:03:44.127 回答
0

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

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

于 2009-09-07T01:14:20.293 回答