用于 MySQLi/MSSQL 行获取的 php DBAL

发布于 2024-09-16 02:31:12 字数 1093 浏览 7 评论 0原文

我正在尝试在 PHP MySQLi(过程)和 MSSQL 库之上编写一个简单、精简且高效的数据库抽象层,以便像这样使用......

while ($a = db::go('db_name')->query('select * from foo')->row()) {
  print_r($a);
}

我已经编写了该类,但在如何最好地使用它方面有些挣扎。循环遍历结果。该类只需要处理选择查询,并且必须处理一些大型结果集。这是 ->row() 方法的片段...

public function row() {
  return $this->{'get'.ucwords($this->details['type']).'Row'}();
}
private function getMysqliRow() {
  return @mysqli_fetch_assoc($this->result);
}
private function getMssqlRow() {
  return @mssql_fetch_assoc($this->result);
}

目前,调用会导致无限循环,并且每次都会检索同一行,因为结果集中的内部指针不会以与 while ($a = mssql_data_seek($result)) 调用相同的方式递增,这完全有意义(结果集不是全局的,您应该能够处理一次多个结果集!)。

那么,有谁知道解决这个问题的优雅而有效的方法吗?或者是使用数据查找功能的唯一解决方案(mysqli_data_seek()< /a> 和 mssql_data_seek()) 并按住指向结果集中当前位置的指针?如果是这样,效率如何(我将处理大型结果集> 250000)? while 循环将如何处理到达结果集末尾的情况?

I'm trying to write a simple, thin and efficient database abstraction layer on top of the PHP MySQLi (procedural) and MSSQL libraries, to be used like this...

while ($a = db::go('db_name')->query('select * from foo')->row()) {
  print_r($a);
}

I have the class written, but am struggling somewhat with how best to loop through the results. The class will only need to handle select queries, and will have to handle some large result sets. Here's a snippet of the ->row() method...

public function row() {
  return $this->{'get'.ucwords($this->details['type']).'Row'}();
}
private function getMysqliRow() {
  return @mysqli_fetch_assoc($this->result);
}
private function getMssqlRow() {
  return @mssql_fetch_assoc($this->result);
}

At the moment, the call is resulting in an infinite loop, and retrieving the same row each time because the internal pointer in the result set isn't incremented in the same way as a while ($a = mssql_data_seek($result)) call would, which makes complete sense (a result set isn't global, you should be able to handle more than one result set at a time!).

So, does anyone know of an elegant and efficient way around this? Or is the only solution to use the data seek functions (mysqli_data_seek() and mssql_data_seek()) and hold a pointer to the current position in the result set? If so, how efficient is this (I will be handling large result sets > 250000)? And how would the while loop handle reaching the end of the result set?

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

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

发布评论

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

评论(1

2024-09-23 02:31:13

忽略这个,是我傻了!我没有将结果资源设为静态成员,因此每次调用时都会重新检索它。抱歉!

Ignore this, it was me being silly! I hadn't made the result resource a static member, so it was being re-retrieved on each call. Apologies!

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