gpt4 book ai didi

php - 调用非对象的成员函数 InnerJoin()

转载 作者:搜寻专家 更新时间:2023-10-30 23:07:47 24 4
gpt4 key购买 nike

我是 PHP 的新手,我正在尝试创建一个具有内部连接功能的数据库类,但我收到了这个错误:

Call to a member function InnerJoin() on a non-object in C:\xampp\htdocs\world\index.php on line 10

我的代码:

<?php 

class DB {
private static $_link = null,
$_host = "127.0.0.1",
$_pass = "",
$_dbname = "mundo",
$_user = "root",
$_charset = "utf8";
private $_pdo,
$_query,
$_count = 0,
$_error = false,
$_results;

private function __construct() {
$this->_pdo = new PDO("mysql:host=".self::$_host.";dbname=".self::$_dbname,self::$_user,self::$_pass);
}

public static function getLink() {
if(!isset(self::$_link)) {
self::$_link = new DB();
}
return self::$_link;
}

public function Get($table) {
return $this->_query = "SELECT * FROM ".$table;
}

public function Go() {
if($this->_query = $this->_pdo->prepare($sql)) {
echo "prepared";
}
}

public function InnerJoin($table1, $column1, $table2, $column2){
return $this->_query = $this->_query." INNER JOIN ".$table2." ON ".$table1.".".$column1." = ".$table2.".".$column2;
}
}
?>

在我的 index.php 中我有这个:

<?php 
$DB = DB::getLink()->Get("paises")->InnerJoin("paises","Id_Continente","Continentes","Id_Continente")->Go();
?>

希望你能帮帮我,谢谢

最佳答案

因为您要从 Get 函数返回 $this->_query,因为它需要返回一个完整的对象才能再次使用它,

public function Get($table) {
$this->_query = "SELECT * FROM ".$table;
return $this;
}

有关详细信息,请阅读 Method Chaining .

关于php - 调用非对象的成员函数 InnerJoin(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24259072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com