2
我正在使用這個類使用mysql,但意識到它已被棄用。我試圖讓它使用PDO或mysqli工作,但我不是100%確定如何讓它工作。使用類sqli或PDO連接到數據庫
class DB {
private $link;
private $host, $username, $password, $database;
public function __construct($host, $username, $password, $database){
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->link = mysqli_connect($this->host, $this->username, $this->password)
OR die("There was a problem connecting to the database.");
mysqli_select_db($this->link,$this->database)
OR die("There was a problem selecting the database.");
return true;
}
public function query($query) {
$result = mysqli_query($link,$query);
if (!$result) die('Invalid query: ' . mysqli_error());
return $result;
}
public function __destruct() {
mysqli_close($this->link)
OR die("There was a problem disconnecting from the database.");
}
public function gettingDepartments(){
$db = new DB("localhost", "root", "", "visitorform");
$result = $db->query("SELECT * FROM tb_user_dept");
return $result;
}
}
這是我的錯誤:
警告:mysqli_query()預計參數1是mysqli的,空給出。 mysqli_error()期望的是1個參數,0給出
你傳遞一個叫'$ link'爲'mysqli_query變量()'但不設置任何地方... –
我猜''結果= mysqli_query($ link,$查詢);'應該可能是'$ result = mysqli_query($ this-> link,$ query);' – cteski
很高興看到有人意識到它的折舊和不完全無視這一事實。 –