Php 在从另一个函数调用的函数中不工作的非对象上调用成员函数bind_param()
我在下面列出的类的get_template()函数中收到上面的错误。有人知道我为什么会收到这个错误吗?所有其他查询都执行得很好,$template_number肯定会返回一个int,这在查询的这一点上是应该的,那么为什么我会收到这个错误呢?这可能是因为这个查询的返回在MySQL中被格式化为文本(并且在PHPMyAdmin中显示为BLOB)Php 在从另一个函数调用的函数中不工作的非对象上调用成员函数bind_param(),php,mysql,mysqli,Php,Mysql,Mysqli,我在下面列出的类的get_template()函数中收到上面的错误。有人知道我为什么会收到这个错误吗?所有其他查询都执行得很好,$template_number肯定会返回一个int,这在查询的这一点上是应该的,那么为什么我会收到这个错误呢?这可能是因为这个查询的返回在MySQL中被格式化为文本(并且在PHPMyAdmin中显示为BLOB) class Page{ private $con; public function __construct(Connection $con) {
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;
}
}
表结构如下所示:
请参阅和。
mysqli\u stmt::execute()
默认情况下存储结果服务器端并逐行获取结果,除非调用mysqli\u stmt::store\u result()
将它们缓冲到客户端。当上一条语句中存在未蚀刻的reults服务器端时,尝试准备另一条语句很可能会失败。对于搜索此解决方案的任何其他人,正如bwewsing建议我需要添加存储结果,但是从上面提供的链接来看,这应该在哪里并不太清楚实现了
实现应该在初始调用方法中进行。因此,当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();
}
大多数情况下,您可以假设对非对象上的成员函数bind_param()的调用
是因为在false
上调用->bind_param
是因为准备失败。我假设这是因为$this->con
。您应该实现mysqli,而不是传入类。已删除[oop],因为将某些东西放入类中并不能使其变为OOP。当然。Owkay。您可以将其称为OOP。在这种情况下,我们也可以将JavaScript称为OOP语言,因为它加载了大量对象。此外,即使它是OOP,也与您的问题毫无关系。@jme1988说您的代码是OOP,因为它使用了类就像说你的车是涡轮模型,因为它有一个扰流板。@jme1988你完全错了,这段代码违反了许多OOP原则。因此,我希望没有新手尝试使用它作为学习OOP的手段,因为如果他们使用你这里的例子,他们将学习一个无效的编程范例。请阅读学习单一责任原则和SOLID的其他组件,也许还可以读一本关于设计模式的书。