jump to navigation

A Simple PHP Mysql Class November 5, 2011

Posted by Tournas Dimitrios in PHP.

This is a simple PHP class to connect to an Mysql database


//Simply include this file on your page 

//Set up all yor paramaters for connection 
$db = new DbConnect("localhost","user","password","database",$error_reporting=false,$persistent=false); 

//Open the connection to your database 
$db->open() or die($db->error()); 

//Query the database now the connection has been made 
$db->query("SELECT * FROM....") or die($db->error()); 

//You have several options on ways of fetching the data 
//as an example I shall use 
while($row=$db->fetcharray()) { 

//do some stuff 


//close your connection 

Class DbConnect { 

var $host = ''; 
var $user = ''; 
var $password = ''; 
var $database = ''; 
var $persistent = false; 

var $conn = NULL; 

var $result= false; 
var $error_reporting = false; 

/*constructor function this will run when we call the class */ 

function DbConnect ($host, $user, $password, $database, $error_reporting=true, $persistent=false) { 

$this->host = $host; 
$this->user = $user; 
$this->password = $password; 
$this->database = $database; 
$this->persistent = $persistent; 
$this->error_reporting = $error_reporting; 

function open() { 

if ($this->persistent) { 

$func = 'mysql_pconnect'; 

} else { 

$func = 'mysql_connect'; 


/* Connect to the MySQl Server */ 

$this->conn = $func($this->host, $this->user, $this->password); 

if (!$this->conn) { 

return false; 


/* Select the requested DB */ 

if (@!mysql_select_db($this->database, $this->conn)) { 

return false; 
return true; 

/*close the connection */ 

function close() { 

return (@mysql_close($this->conn)); 

/* report error if error_reporting set to true */ 

function error() { 

if ($this->error_reporting) { 

return (mysql_error()) ; 


function query($sql) { 

$this->result = @mysql_query($sql, $this->conn); 

return($this->result != false); 

function affectedrows() { 


function numrows() { 


function fetchobject() { 

return(@mysql_fetch_object($this->result, MYSQL_ASSOC)); 

function fetcharray() { 



function fetchassoc() { 


function freeresult() { 




This class includes the following functions :

  • error (mysql_error) : This function is used to get the error message from the last MySQL operation .
  • query (mysql_query) : Query handle for successful SELECT, SHOW, DESCRIBE, EXPLAIN and other statements or FALSE on error.
  • fetcharray (mysql_fetch_array) : The type of the result depends on an optional second parameter :
    MYSQL_ASSOC – Associative array
    MYSQL_NUM – Numeric array
    MYSQL_BOTH – Both associative and numeric array
    Default : MYSQL_BOTH
  • fetchassoc (mysql_fetch_assoc) : Used to retrieve a row of data as an associative array from a MySQL result handle .
    while ($row = mysql_fetch_object(
    ‘SELECT name , surname FROM members’)) {
     echo $row[name] “<br />” ;
    echo $row[surname] “<br />” ;    }
  • fetchobject () : It’s used to fetch a row of data as an object from a result handle .
    while ($row = mysql_fetch_object(
    ‘SELECT name , surname FROM members’)) {
     echo $row->name “<br />”;
    echo $row->surname “<br />”;    }
  • affectedrows (mysql_affected_rows) : Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier
  • numrows (mysql_num_rows) : Retrieves the number of rows from a result set . This command is only valid for statements like SELECT or SHOW that return an actual result set . To retrieve the number of rows affected by a INSERT , UPDATE , REPLACE or DELETE query , use mysql_affected_rows() .
  • freeresult (mysql_free_result) : This function is used to de-allocate memory that was used to store the query results returned by mysql_query() . When your PHP script is finished , PHP will automatically perform garbage collection on the objects and resources you used therein . It is generally not necessary to explicitly disconnect from your MySQL server . However , you should really try to do all you can to conserve memory and resources , and therefore it is smart to explicitly free up your MySQL resources rather than waiting to let PHP do it on your behalf .
  • close (mysql_close) : This also saves computer resources , as with mysql_free_result() ,  it is good to call mysql_close() if you think there will be some time between your last database use and your script ending . No need to supply any parameters to it , as it will automatically close the last-opened MySQL connection .



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s