-3

我在下面列出的类的 get_template() 函数中收到上述错误。有谁知道我为什么会收到这个错误?所有其他查询都执行得很好,并且 $template_number 肯定会返回一个 int ,这是查询中此时预期的,那么为什么我会收到此错误?可能是因为这个查询的返回在 MySQL 中被格式化为 TEXT(并且在 PHPMyAdmin 中显示为 BLOB?

    class Page{

private $con; 

public function __construct(Connection $con) {
    $this->con = $con;
    if(isset($_GET['id'])){
    $id = $_GET['id'];
    }else{      
    $id = 1;
    }       
    $this->get_headers($id);
    $this->get_content($id);
    $this->get_footer($id);
}

private function get_headers($pageId){ 
    $retrieveHead = $this->con->prepare("SELECT headers FROM pages WHERE page_id=?");
    $retrieveHead->bind_param('i',$pageId);
    $retrieveHead->execute();
    $retrieveHead->bind_result($header);
    $retrieveHead->fetch();
    $retrieveHead->close();
    echo $header;   
}

private function get_footer($pageId){ 
    $retrieveFooter = $this->con->prepare("SELECT footer FROM pages WHERE page_id=?");
    $retrieveFooter->bind_param('i',$pageId);
    $retrieveFooter->execute();
    $retrieveFooter->bind_result($footer);
    $retrieveFooter->fetch();
    $retrieveFooter->close();
    echo $footer;   
}

private function get_content($pageId){
    $retreiveContent = $this->con->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence DESC");
    $retreiveContent->bind_param('i',$pageId);
    $retreiveContent->execute();
    $retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
         while ($retreiveContent->fetch()) {
            //Variables will be populated for this row.
            //Update the tags in the template.
            $template = $this->get_template($template_id);
            $template = str_replace('[i1]',$i1,$template);
            $template = str_replace('[i2]',$i2,$template);
            //$theTemplate is populated with content. Probably want to echo here
            echo $template;
        }
    $retreiveContent->close();
}

private function get_template($template_number){
    $retreiveFunction = $this->con->prepare("SELECT code FROM templates WHERE template_id=?");
    $retreiveFunction->bind_param('i',$template_number);
    $retreiveFunction->execute();
    $retreiveFunction->bind_result($template);
    $retreiveFunction->fetch();
    $retreiveFunction->close();
    return $template;
}

}

表结构如下:

表结构

4

2 回答 2

1

这里这里mysqli_stmt::execute()默认情况下,服务器端存储结果并逐行获取,除非您调用mysqli_stmt::store_result()在客户端缓冲它们。尝试准备另一个语句很可能会失败,因为前一个语句有未获取的结果服务器端。

于 2013-02-06T08:33:33.627 回答
-1

对于搜索此解决方案的任何其他人,正如 bwewsing 建议的那样,我需要添加 store_result 但是从上面提供的链接中,不太清楚应该在哪里实施。

实施应在初始调用方法中进行。因此,当 get_content() 方法调用 get_template() 方法时,它应该在此处以下列方式实现:

    private function get_content($pageId){
    $retreiveContent = $this->con->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence DESC");
    $retreiveContent->bind_param('i',$pageId);
    $retreiveContent->execute();
    $retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
    $retreiveContent->store_result();
         while ($retreiveContent->fetch()) {
            //Variables will be populated for this row.
            //Update the tags in the template.
            $template = $this->get_template($template_id);
            $template = str_replace('[i1]',$i1,$template);
            $template = str_replace('[i2]',$i2,$template);
            //$theTemplate is populated with content. Probably want to echo here
            echo $template;
        }
    $retreiveContent->free_result();    
    $retreiveContent->close();
}
于 2013-02-06T20:01:06.723 回答