這是我的類,用於連接到數據庫,但是當我創建該類的實例時,它給了我這個錯誤:Fatal error: Call to undefined method DB::query()
在C:\ Apache24 \ htdocs \ core \ DB.class.php上第21行從MySQL數據庫獲取結果時出錯
<?php
class DB {
protected $db_name = "data_db";
protected $db_user = "root";
protected $db_pass = "root";
protected $db_host = "localhost";
public function __construct() {
$db = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
if ($db->connect_errno) {
printf("Connection failed: %s\n", $db->connect_error);
exit();
} else
return $db;
}
}
$db = new DB();
$data_row = $db->query("SELECT * FROM `users`");
while($data = $data_row->fetch_assoc())
echo $data["first_name"] . '<br>';
?>
致命錯誤:調用未定義的方法DB ::查詢()在C:\ Apache24 \ htdocs中\核心\上線DB.class.php 21
您缺少該方法...創建一個名爲query的方法,並將您的邏輯放在那裏。 – VikingBlooded
構造函數不返回值,它們返回類的實例。 – Danijel