This feature is not available (in other words still being worked on!) - check back soon!
How-To: Make a MySQL Class… The Easy Way!
Posted by Ben Winn in Uncategorized on June 14th, 2010

As we all know it’s always time consuming to code a script to connect to a MySQL database, and I probably speak for most web developers that we like a easy way of doing this.

So what is that easy way of doing it you ask? Well simple we make a class-object that can be used on many PHP scripts without needing to write loads of lines just to connect to a database.

First up we need make a php class-object file, which we could use on another PHP script just by including it before we perform our connection.

MySQL.Class.php

<?php

//MySQL Class

class mySQL{

private $dbConn;
private $dbSql;
private $dbQuery;
private $dbBind = array();

//connection

public function connect($dbHost, $dbName, $dbUser, $dbPass)
{
   $this->dbConn = mysql_connect(
       $dbHost,
       $dbUser,
       $dbPass
   ) or die(mysql_error());
   mysql_select_db($dbName, $this->dbConn) or die(mysql_error());

public function prep($sql)
{
    $this->dbSql = $sql;
}

public function bind($hook, $value)
{
   $this->dbBind[$hook] = $this->escape($value);
}

private function escape($value)
{
    if(get_magic_quotes_gpc())
        $value = stripslashes($value);
    return mysql_real_escape_string($value, $this->dbConn);
}

public function run()
{
    $sql = $this->;dbSql;
    if(is_array($this->dbBind))
        foreach($this->dbBind as $hook => $value)
           $sql = str_replace($hook, "'" . $value . "'", $sql);
    $this->dbQuery = mysql_query($sql) or die(mysql_error());
    $this->dbBind = array();
    return $this->numRows();
}

public function fetchAll($type = MYSQL_ASSOC)
{
   $tmpArr = array();
   while($row = mysql_fetch_array($this->dbQuery, $type))
   {
      $tmpArr[] = $row;
   }
   return $tmpArr;
}

public function fetchAssoc()
{
  return mysql_fetch_assoc($this->dbQuery);
}

public function numRows()
{
  return mysql_num_rows($this->dbQuery);
}

public function insertId()
{
  return mysql_insert_id($this->dbConn);
}

?>

Now how we do call this script in another PHP document? Well that’s easy let me show you:

Create an example PHP document name it whatever you like, for me I’ll be calling it “simplemysql.php”:

SimpleMySQL.php

<?php

require_once("MySQL.Class.php"); // This the MySQL Class file

// Start by using the 'New' namespace to start a new instance of our MySQL Class

$simple = new mySQL();

// Now here's where we'll provide our database info -- here I'll provide fake info but change these values to your actual database login info

$connectit = $simple->connect('localhost','testdb','testuser','123456');

// Now that's done we'll create a simple error method, not the most effective but fits this purpose!

if (!$connectit) {
echo 'We were unable to connect to the database at this time... Please contact the Administrator!';
}
else
{
}

?>

Now there isn’t much to say about the above script as it’s that really simple, it simply starts a new instance of our MySQL class and we start another line which sends a command to the class to start a new connection and then the last is if for some reason it fails the user will see our custom error message, this can be changed to whatever you like so go ahead and change it :)

I wouldn’t say this whole script is 100% security proof but it has it’s certain level of security but if you want 100% security-proof then you will need to do this off your own back as this is only a snippet/tutorial and not a full fledged script.

However, if you encounter any problems then comment below and I’ll try and help or if not I’m sure other users may help you.

Thanks for reading!

Tags:
Comments Off

Comments are closed.