PHP/MySQL Sample Database Class

18 Mar
class MySqlDatabase
private $_connection;
public $last_query;
private $magic_quotes_active;
private $real_escape_string_exists;

public function __construct()
/* Returns the current configuration setting of magic_quotes_gpc.
Returns 0 if magic_quotes_gpc is off, 1 otherwise. */
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exists = function_exists("mysql_real_escape_string");

public function open_connection()
$this->_connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
die('Database connection failed'.mysql_error());
$db_select = mysql_select_db(DB_NAME,$this->_connection);
die('Database selection failed'.mysql_error());

public function close_connection()

/* For SELECT returns resultset on success, false on error.
* For INSERT, UPDATE, DELETE etc returns true on success, false on error */
public function query($sql)
$this->last_query = $sql;

$result = mysql_query($sql,$this->_connection);
return $result;

/* Make sure query executed successfully.
* Only for debugging purposes. */
private function confirm_query($result)

$output = "Databse query failed".mysql_error()."<br>";
$output .= "Last query ".$this->last_query;


/* Escape special characters in the string before sending to the database
* Consider PHP Version*/
public function escape_value($value)

if($this->real_escape_string_exists)// PHP version 4.3.0 or higher

/* Automatically add back slashes when magic quotes are active in php.ini
* First remove them. */
$value = stripslashes($value);
//Escapes special characters in a string
$value = mysql_real_escape_string($value);
else //before PHP version 4.3.0

// Add slashes manually
$value = addslashes($value);

return $value;

/* Returns an array of strings that corresponds to the fetched row.
* Returns FALSE if there are no more rows.*/
public function fetch_array($result_set)
return mysql_fetch_array($result_set);

/* Retrives the number of rows from the result set.
* Returns FALSE on failure. */
public function num_rows($result_set)
return mysql_num_rows($result_set);

/* Get the ID generated in the last query
* 0 if the previous query does not generate an AUTO_INCREMENT value.
* FALSE if no MySQL connection was established.  */
public function insert_id()
return mysql_insert_id($this->_connection);

/* Get number of affected rows in previous MySQL operation.
* Return -1 if the last query failed.  */
public function affected_rows()
return mysql_affected_rows($this->_connection);

//Creates an object from MySQLDatabase class
$gdbObj = new MySqlDatabase();

Posted by on March 18, 2012 in OOP, PHP



8 responses to “PHP/MySQL Sample Database Class

  1. jay

    April 16, 2012 at 10:02 pm

    Good tutorial thanks for sharing

  2. Naseer

    May 5, 2012 at 12:18 am

    Very useful tutorial. I copied the class to use with my projects.
    Thanks for posting here.

    • Rajitha

      May 5, 2012 at 10:06 am

      Thank you Naseer.

  3. phillipe

    May 10, 2012 at 1:45 am

    Your page is so beautifull.

  4. Nico

    May 20, 2012 at 10:34 am


  5. Ndaru

    May 21, 2012 at 7:16 am

    Simple, clean, beautiful code. Thanks a bunch.

  6. maxanda

    May 29, 2012 at 1:09 am

    I love how clean this code is. As I am very new to OOP in PHP (and to advanced PHP in general), the comments really help me out a great deal in understanding how this works. There is one thing I would like to ask, if you don’t mind.

    I noticed you used MySQL instead of MySQLi. I am using a server I installed on my computer for experimentation and testing (with everything up to date) and I would like to know … if you were to use MySQLi instead, would it be better to keep it in procedural style like in the code above or OOP style like the rest of the code? ie. mysqli_close($mysqli) vs $mysqli->close()

    • Rajitha

      May 29, 2012 at 9:01 am

      Thanks maxanda for commenting this tutorial. I recommend to use mysqli instead of mysql. It is an object-oriented interface to the MySQL bindings which makes things easier to use. It also offers support for prepared statements.Prepared statements is good because it eliminates SQL injection possibilities.
      MySQL PDO extension is an alternative for MySQLi. The old mysql extensions that I use in this tutorial are not recommended for new development.


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: