PHP provides inbuilt functions to do the MySQL database operations and we can use the functions in traditional way. But the following is an example of extended class which can be used for the same purpose.
/*************************************************** * Define the database parameters (Usually defined in configuration file) ****************************************************/ define("DBHOST", "DB_HOST");//Database Host define("DBUSER", "DB_USER");//Database User Name define("DBPASS", "DB_PASS");//Database Password define("DBNAME", "DB_NAME");//Database Name /***************************************************/ /************************************************* * Purpose : Database Connection class **************************************************/ //Database Connection parameteres from the configuration file class dbConnectionParameter { private $dbHost; private $dbUser; private $dbPassword; private $dbName; //Get & Set Host protected function fn_SetDBHost($dbHost) { $this->dbHost = $dbHost; } public function fn_GetDBHost() { return $this->dbHost; } //Get & Set Username protected function fn_SetDBUser($dbUser) { $this->dbUser = $dbUser; } public function fn_GetDBUser() { return $this->dbUser; } //Get & Set Password protected function fn_SetDBPassword($dbPassword) { $this->dbPassword = $dbPassword; } public function fn_GetDBPassword() { return $this->dbPassword; } //Get & Set Database name protected function fn_SetDBName($dbName) { $this->dbName = $dbName; } public function fn_GetDBName() { return $this->dbName; } public function fn_SetParam() //Set parameters { $this->fn_SetDBHost(DBHOST); $this->fn_SetDBUser(DBUSER); $this->fn_SetDBPassword(DBPASS); $this->fn_SetDBName(DBNAME); } } //MySql connection using the dbConnectionParameter as a extended class class dbConnection extends dbConnectionParameter { private $dbCoonection; private $rsResultset; private $arrResult; public function fn_ConnectionOpen() //Connection open { parent::fn_SetParam(); $this->dbCoonection = mysql_connect(parent::fn_GetDBHost(), parent::fn_GetDBUser(), parent::fn_GetDBPassword()) or die('Not connected : ' . mysql_error()); mysql_select_db( parent::fn_GetDBName(), $this->dbCoonection) or die('Not connected : ' . mysql_error()); } public function fn_MysqlQuery($sqlQuery) //mysqli_query { $this->rsResultset = mysql_query($sqlQuery) or die('Mysql Error : ' . mysql_error()); return $this->rsResultset; } //Fetch associate from the result set public function fn_MysqlFetchAssoc($rsResultset) { $this->arrResult = mysql_fetch_assoc($rsResultset); if($this->arrResult) { return $this->arrResult; } else { return false; } } public function fn_ConnectionClose() //Close connection { if($this->dbCoonection) { mysql_close($this->dbCoonection) or die('Mysql Error : ' . mysql_error()); } } } /*************************************************** * End Of Database Connection class ****************************************************/ /*************************************************** *Use the class to do the database operation ****************************************************/ $dbConnnection = new dbConnection(); $dbConnnection ->fn_ConnectionOpen(); $strQuery = "SELECT COL1, COL2, COL3 FROM TABLE_NAME"; $rsResultSet = $dbConnnection ->fn_MysqlQuery($strQuery); while ($row = $dbConnnection ->fn_MysqlFetchAssoc($rsResultSet)) { echo $row[COL1]; echo "<br>"; echo $row[COL2]; echo "<br>"; echo $row[COL3]; echo "<br>"; } $dbConnnection ->fn_ConnectionClose(); /****************************************************/