2
我想創建一個類,需要與數據庫進行交互的各種方法,所以我想在__construct中使用mySQLi創建一個數據庫對象,具體取決於我是否還在有一個數據庫連接或沒有,但我不確定我怎樣才能從一個方法中,因爲我是新來的面向對象的PHP訪問數據庫對象,這裏是我的代碼,從方法訪問mySQLi數據庫對象
class my_class
{
private $db_host = '';
private $db_user = '';
private $db_pass = '';
private $db_name = '';
function __construct() {
$myconn = new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name);
if($myconn) {
$this->con = true;
return true;
} else {
return false;
}
}
private function my_function(){
//not sure how I can access the database object from here to run a query
}
}
我如何能有什麼建議改善這一點,或以更好的方式接近這一點,將不勝感激!
請原諒我對這個問題的無知!
Thanx提前的傢伙!