【发布时间】:2012-11-18 05:50:08
【问题描述】:
我一直在寻找这个问题的答案已经有一段时间了,但似乎以前没有人解决过这个问题。也许你们中的一些人能够并且愿意在这方面帮助我……那太好了!
我目前正在开发一个 mysqli 包装器,并尝试为准备好的语句实现一个自定义结果类,就像我已经为标准查询所做的那样!似乎结果是在 stmt 的“执行”方法中生成的,但我仍然无法理解幕后发生的事情!
有没有办法(或破解)将生成的结果指向我的结果类,而不是像常规查询那样使用普通的 mysqli_result?
只是为了让你有个想法,这里是代码中的一点粘贴:
class extended_mysqli extends mysqli
{
public function __construct()
{
call_user_func_array(array(get_parent_class($this), 'mysqli'), func_get_args());
if ( $this->connect_errno )
{
throw new extended_mysqli_exception('database connection failure');
}
}
public function query ($query, $binds = array())
{
if ( empty( $binds ) )
{
if ( $this->real_query($query) )
{
if ( $this->field_count )
{
return new extended_mysqli_result($this, $query); // select, show, describe
}
else return true; // insert, update, delete
}
else return false; // fix
}
else
{
$stmt = $this->prepare($query);
if ( $stmt->bind_array($binds) )
{
return $stmt->execute() ? $stmt->get_result() : false;
}
else return false;
}
}
public function prepare($query)
{
return new extended_mysqli_stmt($this, $query);
}
// ...
}
class extended_mysqli_stmt extends mysqli_stmt
{
public function __construct($link, $query)
{
parent::__construct($link);
$this->prepare($query);
}
public function execute()
{
// what do i do here ???
}
}
class extended_mysqli_result extends mysqli_result implements countable, iterator, arrayaccess
{
public function __construct($link, $mode = MYSQLI_STORE_RESULT)
{
parent::__construct($this->link = $link, $mode);
}
// ...
}
【问题讨论】:
-
不确定我是否理解你。您希望结果的数据结构是什么?
-
这与数据结构无关……如果我不能使用我的 extended_mysqli_result,我会错过很多功能!假设我想执行 $stmt->get_result();我得到的结果仍然是 mysqli_result 而不是我写的扩展的!
-
为什么你要扩展mysqli?你真正想要完成的是什么?
-
我错过了一些默认情况下未实现但仍然可以很好地应用于结果集的便捷功能......因为我不在 php5.4 环境中,例如,拥有可转换的结果集可能会很好!我曾经执行不依赖于 stmts 的查询,并且我的框架中有一些设施,现在我无法实现,因为出于安全原因,我试图使用它们!
-
你能不能不只是用
parent::execute()调用父类的execute()方法,然后按你的意愿处理fetch调用?