调用非对象上的成员函数bind_param(),该对象在从另一个函数调用的函数内部不起作用 [英] Call to a member function bind_param() on a non-object not working inside function called from another function
问题描述
我在下面列出的类的get_template()函数中收到上述错误.有谁知道我为什么收到此错误?所有其他查询都可以正常执行,并且$ template_number肯定返回查询中目前期望的int值,为什么我会收到此错误?可能是因为该查询的返回在MySQL中的格式为TEXT(在PHPMyAdmin中显示为BLOB)?
I'm receiving the error above in the get_template() function of my class listed below. Does anyone know why I am receiveing this error? All of the other queries execute fine and $template_number is definitely returning an int which is expected at this point in the query so why am I receiving this error? Could it be because the return on this query is formatted as TEXT in MySQL (and appears as a BLOB in PHPMyAdmin?
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;
}
}
表结构可以在下面找到:
Table structure can be found below:
推荐答案
对于其他正在寻找此解决方案的人,正如bwewsing建议我需要在store_result中添加,但是从上面提供的链接中尚不清楚应该在何处实现.
For anyone else searching for this solution, as bwewsing suggests I needed to add in store_result however from the links provided above it's not too clear where this should be implemented.
实现应在初始调用方法中进行.因此,当get_content()方法调用get_template()方法时,应在此处以以下方式实现:
Implementation should take place in the initial calling method. So, as the get_content() method calls the get_template() method it should be implemented here in the following manner:
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();
}
这篇关于调用非对象上的成员函数bind_param(),该对象在从另一个函数调用的函数内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!