MYSQLi bind_result分配了太多内存

时间:2011-02-19 19:01:26

标签: php mysql mysqli prepared-statement

我正在尝试从MYSQL中获取多行,但是当将变量绑定到结果时,MYSQLi会在内存耗尽时进行分配,因为它会尝试一次性获取所有行,并且即使在没有必要时也会缓冲完整的LONGBLOB大小。 / p>

还讨论了错误here。一张海报似乎已经使用mysqli_stmt_store_result解决了问题,但没有详细说明(和mysqli_stmt_store_result是一种程序性(非OO)方法。

  

致命错误:允许的内存大小为   耗尽134217728个字节(试过   分配4294967296个字节)

理想情况下,我更喜欢使用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;
  }

感谢您的帮助!

3 个答案:

答案 0 :(得分:5)

这不是MEDIUM而是长文字尺寸 检查你的表定义

答案 1 :(得分:2)

我已使用以下代码解决了这个问题。仍有一个问题,因为一些返回的数据出现了损坏,但我认为这值得提出自己的问题。对我来说棘手的是需要在mysqli对象上调用store_result(),而需要在语句中调用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);
  }

答案 2 :(得分:0)

mysqli_stmt_store_result has an OO form as well