如何在 PHP 中抽象 mysqli 准备好的语句?
我正在使用自己的类进行数据库查询,扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在
Zend_Db_Adapter_Mysqli
和Zend_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
andZend_Db_Statement_Mysqli
classes quite a bit to get this to work, since we wanted to make it conform to thePDO
andPDOStatement
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 byPDOStatement
.If you want to see the code in
Zend_Db
, pay special attention to the functionsZend_Db_Statement_Mysqli::_execute()
andfetch()
. Basically, the_execute()
method binds an array of variable references usingcall_user_func_array()
. The tricky part is that you have to initialize the array so thebind_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.
我会看一下 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.
看来你必须按照你所说的“我需要使用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.