Forum Moderators: coopster
Am I doing something wrong here? The class is working fine, its just that i need to get used to the code and I'm not sure if im programming this properly using oop.
$database = "vbox";
$dbmod = New database;
$dbmod->__construct();
$zzz = "show tables";
$dbmod->__query($zzz);
$test = $dbmod->sqlError;
echo $test;
class database {
private $database= false;
public $sqlError= false;
public $rowCount= false;
public $insertID= false;
public function __construct() {
global $database;
$this->database = $database;
if (!mysql_connect()) $this->sqlError = 'Cannot connect to the database because: '.mysql_error();
elseif (!mysql_select_db($database)) $this->sqlError = 'Cannot select database because: '.mysql_error();
}
public function __destruct() {
mysql_close();
}
public function query($query) {
$this->sqlError = false;
$this->foundRows = false;
$this->rowCount = false;
$this->insertID = false;
$result = mysql_query($query);
if (mysql_error()) {
$this->sqlError = 'Query error: ' . mysql_error();
debug($query.'<br>'.$this->sqlError);
return false;
} else {
$this->rowCount = strpos($query, 'SELECT') === 0? mysql_num_rows($result) : mysql_affected_rows();
if (strpos($query, 'INSERT') === 0) $this->insertID = mysql_insert_id();
if (strpos($query, 'SQL_CALC_FOUND_ROWS')!== false) {
$this->foundRows = mysql_result(mysql_query("SELECT FOUND_ROWS()"), 0);
}
debug($query.'<br>Found Rows: '.$this->foundRows.' ¦ Row Count: '.$this->rowCount.' ¦ Insert ID: '.$this->insertID);
return $result;
}
}
} // class database
As for __construct being called automatically, is this standard for all classes that __construct is called automatically?
For PHP5, yes, unless the parent class has been extended.
Constructors and Destructors [php.net]