I am building a PHP framework and would like to get some feedback on a few different sections of the project so far. I consider myself still a neophyte in PHP so I would like to ask if I'm going about completing these different tasks in an efficient and or correct way.
This section is of the MySQL Connection class I have created for it. So for I have created the basic operations for connecting and querying the database. I have posted below the class code and a script showing how it can be use.
I would like to get feedback on if this is the proper way to create this type of class and an efficient way to complete database querying tasks.
MySQL Connection Class
class MysqlConnection{ //Connetion login private $host; private $user; private $password; private $database; private $connection; //Query being used public $query; /** * @param String $host - server host * @param String $user - username * @param String $database - database name * @param String $password - username password * @return Null */ public function __construct($host,$user,$password,$database){ if(isset($host,$user,$password,$database)){ //If parameter supplied $this->host = $host; $this->user = $user; $this->password = $password; $this->database = $database; }else{ return false; } } /** * Send query to database * * @param String $query - query statment * * Note: This allow a query to the DB if * the class is going to be used withing a fuction * or method */ public function Query($query){ if(isset($this->connection)){ if(($this->query = @$this->connection->query($query))){ return $this->query; } return false; }else{ return false; } } public function LastId(){ return $this->connection->insert_id; } /** * Result */ public function Result($quresults = null){ if(empty($quresults) && !empty($this->query)){ return $this->query->fetch_assoc(); } return $quresults->fetch_assoc(); } /** * Inserts a new row into the database. * * @param Array $data - key column name value data * @param String $table - name of DB table * @return results on success else mysql error string on failure * */ public function insert($data, $table) { $columns = ""; $values = ""; foreach ($data as $column => $value) { $columns .= ($columns == "") ? "" : ", "; $columns .= $column; $values .= ($values == "") ? "" : ", "; $values .= "'".addslashes($value)."'"; } $sql = "insert into $table ($columns) values ($values)"; if(isset($this->connection)){ if(!($this->query = @$this->connection->query($sql))){ return false; } return $this->query; }else{ trigger_error('MySQLConnection not opened',E_USER_ERROR); } } /** * Update a row in DB * * @param Array $data - key is the column name and value is data * @param String $table - name of table to update row * @param string $where - where clause */ public function update($data, $table, $where) { if(isset($this->connection)){ foreach ($data as $column => $value) { $sql = "UPDATE $table SET $column = '".addslashes($value)."' WHERE $where"; if(!@$this->connection->query($sql)){ return false; } } return true; }else{ trigger_error('MySQLConnection not opened',E_USER_ERROR); } } /** * Select rows from the database * * */ public function select($select = '*',$table, $where) { if(isset($this->connection)){ $sql = "SELECT ".$select." FROM $table WHERE $where"; if(($result = $this->connection->query($sql))){ if($result->num_rows > 0){ return ($this->query = $result); } return false; } return false; }else{ trigger_error('MySQLConnection not opened',E_USER_ERROR); } } /** * Delete row from */ public function delete($table,$where){ if(isset($this->connection)){ $sql = "DELETE FROM ".$table." WHERE ".$where; $this->connection->query($sql); if($this->connection->affected_rows > 0){ return true; } return false; } } /** * Close DB connection * * @param void * @return void */ public function Close(){ $this->connection->close(); } /** * Open DB connection using parameters given * in the construct * * @param void * @return boolean - TRUE on success else FALSE on failure */ public function Open(){ $this->connection = @new mysqli($this->host,$this->user,$this->password,$this->database); //return !!$this->connection->connect_errno; if($this->connection->connect_errno){ return false; }else{ return $this->connection; } } /** * Get last DB connection or query error * * @param void * @return DB connection or query error string * * Dev Note: Modify method to allowing opionsel * return value */ public function GetError() { if(!empty($this->connection->connect_error)){ return $this->connection->connect_error; }elseif(!empty($this->connection->error)){ return $this->connection->error; } } /** * Escape special characters by * adding slashes * * @param String - string to escape characters * @return String - escape string */ public function AddSlashes($string){ return addslashes($string); } /** * Break an array up into comma seperated value list * * @param Array $array - an array or strings * @return String of CSV list */ public function PBreak($array){ if(is_array($array)){ return implode(',', $string); } } /** * Close open DB connections * * @param void * @return void */ public function __destruct(){ if(!empty($this->connection)){ @$this->connection->close(); } } }
Script using it's methods
//Conntect to DB $dbconnection = $core->CreateCore('mysqlconnection','database','localhost','root','password','dummy'); $dbconnection->Open(); $queryReturn = $dbconnection->Query("SELECT * FROM dummy WHERE num = 1"); echo "<pre>"; //Using Result() var_dump($dbconnection->Result()); echo "</pre>"; /** * equivalent using QuerySelecti * * The dummy table columns are * num INT, text VARCHAR, other VARCHAR */ //Using insert() method $dbconnection->insert(array('num'=>4,'text'=>'New insert','other'=>'This is a text'),'dummy'); //Using LastId() method echo "<pre>"; //Using Result() var_dump($dbconnection->LastId()); echo "</pre>"; //using GetError() method if(!$dbconnection->GetError()){ $dbconnection->update(array('num'=>4,'text'=>'Another insert','other'=>'This is NEW text'),'dummy','num = 4'); } //using select() method $queryReturn = $dbconnection->select("num, text, other",'dummy','num = 4'); echo "<pre>"; var_dump($dbconnection->Result()); echo "</pre>"; //Using update method $dbconnection->delete('dummy','num = 4'); //Using close() method $dbconnection->Close();
Any feedback would be appreciated.
parent::__construct();
in the constructor, but the class itself:class MysqlConnection{
doesn't extend anything. If it extendsmysqli
(as your tags suggest), then don't. Simply don't. Also adhere to the coding standards, and stick to the SOLID principles. and stop using the@
operator of death\$\endgroup\$DatabaseConnection
class that then extends aSystem
class that reads default database connect values from am ini file. I have posted a question about theSystem
class if you would like to review it. codereview.stackexchange.com/q/63687/28060 I hope this was a good answer. I can post the code for theDatabaseConnection
class if you think it would help.\$\endgroup\$