Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 8 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Critique my first OOP attempt MySQL connection class

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 verifyDatabaseConnectionverifyDatabaseConnection method, I used an @@ symbol to curb the error I was receiving.

Critique my first OOP attempt

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.

MySQL connection class

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.

Post Migrated Here from stackoverflow.com (revisions)
Source Link
user738910
user738910

Critique my first OOP attempt

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");
 ?>