今天我試圖將我的代碼轉換爲PHP/MySQLi OOP代碼。PHP代碼轉換爲PHP/MySQLi OOP
class Database
{
private $host;
private $user;
private $password;
private $db;
private $mysqli;
function __construct()
{
$this->host = "*****";
$this->user = "*****";
$this->password = "******";
$this->db = "*****";
$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
if (mysqli_connect_errno()):
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
endif;
}
}
這是查詢的腳本:
include_once("WD_Config/database.php");
class Adressen_Db
{
function __construct()
{
$this->database = new Database();
}
public function selecteer()
{
$query = "SELECT * FROM wd_adressen WHERE verborgen = 0 ORDER BY naam ASC";
$result = $this->database->mysqli->query($query);
return $result;
}
}
這也是我怎麼稱呼它。
$adressen = new Adressen_Db;
$adressen_result = $adressen->selecteer();
echo "<p>";
while ($row = $adressen_result->fetch_assoc()):
echo "<a href='http://maps.google.com/?q=".$row['voladres']."' target='_blank'>".$row['naam']."</a> woonachtig op <i>".$row['voladres']."</i><br>";
endwhile;
echo "</p>";
我總是得到一個「調用一個非對象的成員函數query()」。不要緊,我特里...
有人可以告訴我爲什麼這是?
謝謝!
您需要更改爲公開。 'private $ mysqli;'到'public $ mysqli;'。 –
'Database :: mysqli'是私人的 – PeeHaa
謝謝戴夫,這確實有效!但不應該$ mysqli總是私人的? –