I am trying to learn OOP using PHP5 and I wrote a simple MySQL connection class. Please take a look and give me some feedback on better practices, critical errors, and any other feedback you can provide.
Note: in the verifyDatabaseConnection
method, I used an @
symbol to curb the error I was receiving.
<?php
class Mysql
{
private $user;
private $pass;
private $data;
private $host;
public function __construct($user,$pass,$data,$host)
{
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->host = $host;
$this->verifyNullFields();
}
private function verifyNullFields()
{
if($this->user == NULL)
{
print('mysql error : username is null');
}
if($this->data == NULL)
{
print('mysql error : database name is null');
}
else if($this->host == NULL)
{
print('mysql error : host name is null');
}
else
{
$this->verifyDatabaseConnection();
}
}
private function verifyDatabaseConnection()
{
$link = @mysql_connect($this->host,$this->user,$this->pass);
if(!$link)
{
die('mysql error : databse connection issue');
}
else
{
$this->verifyDatabaseExist();
}
}
private function verifyDatabaseExist()
{
$db = mysql_select_db($this->data);
if(!$db)
{
die('mysql error : database selection issue');
}
}
}
?>
<?php
$m = new Mysql("root","","test","localhost");
?>