php - MYSQLi bind_result 分配太多内存

标签 php mysql mysqli prepared-statement

<分区>

我正在尝试从 MYSQL 中获取多行,但是当将变量绑定(bind)到结果时,MYSQLi 会用完内存来分配,因为它会尝试一次获取所有行并缓冲完整的 LONGBLOB 大小,即使在不需要时也是如此。

错误也讨论了here .一位张贴者似乎已经使用 mysqli_stmt_store_result 解决了这个问题,但没有详细说明具体如何(mysqli_stmt_store_result 是一种过程性(非 OO)方法。

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4294967296 bytes)

理想情况下,无论如何我更愿意使用 fetch_object(),但我不知道如何让它与我准备好的语句一起运行。

  public function display() {
    $page_offset = ($this->get_page_number()- 1)
                      * $this->notes_per_page;
    if ($page_offset < 0) {
      $page_offset = 0;
    }
    $sql = "SELECT title, date_posted, text, url
              FROM notes ORDER BY date_posted DESC
              LIMIT ?, ?";
    $results = $this->query($sql, "ii", $page_offset, $this->notes_per_page);
    $results->bind_result(&$title, &$date_posted, &$text, &$url);
    //while ($row = $result->fetch_object()) { //store_result()) {
      //echo 'success';
      //var_dump($row);
    //}
    //$this->write($results);
  }

  // Here is the query function that $this->db->query() above refers to.
  public function query() {
    $args = func_get_args();
    $statement = $this->db->prepare($args[0]);
    $args = array_slice($args, 1);
    call_user_func_array(array($statement, 'bind_param'), &$args);
    $statement->execute();
    return $statement;
  }

感谢所有帮助!

最佳答案

我已经使用以下代码解决了这个问题。仍然存在一个问题,因为一些返回的数据看起来被破坏了,但我相信这值得自己的问题。对我来说棘手的是 store_result() 需要在 mysqli 对象上调用,而 fetch_object() 需要在语句上调用。

  public function display() {
    $page_offset = ($this->get_page_number()- 1)
                      * $this->notes_per_page;
    if ($page_offset < 0) {
      $page_offset = 0;
    }
    $sql = "SELECT title, date_posted, text, url
              FROM notes ORDER BY date_posted DESC
              LIMIT ?, ?";
    $results = $this->query($sql, "ii", $page_offset, $this->notes_per_page);
    $results = $this->db->store_result();
    while ($row = $results->fetch_object()) {
      var_dump($row);
    }
    //$this->write($results);
  }

关于php - MYSQLi bind_result 分配太多内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052870/

相关文章:

javascript - 数据表排序日期 (d/m/Y H :i A)

php - Wordpress 的自定义用户字段 - 最好的解决方案是什么?

php - 如何正确搜索php

mysql - 连接到外部 MySQL 数据库错误 mysqli_connect() : (HY000/2003)

php - MySQLi 和 PDO 学什么比较好?

php - Heroku PHP 应用程序无法写入 cedar 堆栈上的文件系统

php - 网站后门 & eval()

java - 在 Hibernate 中使用 MySQL RDS 启用 SSL

php - 如何从 MySQL 数据库中检索图像并显示在 html 标签中

php - 如果我在 PHP 中使用静态方法创建到数据库的连接,我最终会得到一个还是多个连接?