我继续收到此错误
在.on第134行的非对象上调用成员函数prepare()。
database.php中
require_once("connect.php");
class Database {
private $connect;
function one() {
$connectdb = $this->connect;
$forms = new Forms();
$forms->two();
}
}
forms.php
require_once("connect.php");
class Forms {
private $connect;
function two() {
$connectdb = $this->connect;
$construct = $connectdb->prepare("SELECT child.*, parent.name as pname, parent.information as pinformation
FROM strings child LEFT JOIN strings parent on child.pageid=parent.id WHERE child.sid=:id AND child.submittype='2'");
$executequery = $construct->execute(array(':id'=>$id));
}
}
错误发生在查询行上。我的猜测与私有变量$connect
有关。我该如何解决这个问题?
答案 0 :(得分:0)
您需要首先实例化类Database
的新对象并将其分配给$connect
:
$this->connect = new Database();
然后你可以使用它:
$connectdb = $this->connect;
答案 1 :(得分:0)
Forms类中的Connect不是数据库连接器。
class Forms {
private $connect;
public function __construct(){
$this->connect = Database::getConnection(); // we will define this method below
}
function two() {
$connectdb = $this->connect;
$construct = $connectdb->prepare("....query....");
$executequery = $construct->execute(array(':id'=>$id));
}
}
数据库连接器:
class Database {
private static $connect;
public static function getConnection(){
if (self::$connect){
return self:$connect;
}
self::$connect = new mysqli(...) or what ever connection u use
}
}