QAds
Nokia N900 Phone/Mobile Computer
$412.49
Apple iPod shuffle 4 GB Pembe
$49.13
Samsung I7500 Android 5 MP Camera, Wi-Fi, and 8 GB Memory
$599.99
Motorola RIZR Z8 Ferrari Quad-Band, 2 MP Camera, MP3 Player and MicroSD Slot
$194.99
:: HOME PHP Database with PHP Class
 
Database with PHP Class

How to connect MySQL database (db) and do MySQL database transactions with PHP db class:
db.PHP - MySQL db class file :
<?PHP
class database
{
    var $host;
    var $dbname;
    var $user;
    var $password;
    var $connection;
    var $query;
    var $query_no;
    var $debug;

    function database()
    {
        $this->host="localhost"; // Type your db host address
        $this->dbname="DBNAME"; // type your database name
        $this->user="DBUSER"; // type your database user
        $this->password="DBPASSWORD"; // type your database user password
        $this->connection = 0;
        $this->query_no = 0;
        $this->debug = 0; // if you type one, it will show your error messages, else it doesnt. if you work on local server type it to 1, if you work on server type it 0
    }

    function host($hostname)
    {
        $this->host=$hostname;
    }

    function dbname($databasename)
    {
        $this->dbname=$databasename;
    }

    function user($dbuser,$userpwd)
    {
        $this->user=$dbuser;
        $this->password=$userpwd;
    }

    function sql_connect()
    {
        if($this->dbname=="")
            $this->error("Unknown Database");

        $this->connection=@MySQL_connect($this->host,$this->user,$this->password);

        if(!$this->connection)
            $this->error("Connection Problem");

        if(!@MySQL_select_db($this->dbname,$this->connection))
            $this->error("Unknown Database");
    }

    function sql_close()
    {
        if($this->connection)
            MySQL_close($this->connection);
    }

    function query($sqlquery)
    {
        if(!$this->connection)
            $this->sql_connect();

        $this->query[++$this->query_no]=@MySQL_query($sqlquery,$this->connection);
       
        if(!$this->query[$this->query_no])
            $this->error("<br>".$sqlquery.":<br>".MySQL_error());
           
        return $this->query_no;
    }

    function num_rows($query_no=-1)
    {
        if($query_no==-1) $query_no=$this->query_no;

        if((!$this->connection) || (!$this->query[$query_no]))
            $this->error("Unknown Query -sql-num-rows-!!!");

        return MySQL_num_rows($this->query[$query_no]);
    }

    function fetch_array($query_no=-1)
    {
        if($query_no==-1) $query_no=$this->query_no;

        if((!$this->connection) || (!$this->query[$query_no]))
            $this->error("Unknown Query -fetch-!!!");

        return MySQL_fetch_array($this->query[$query_no]);
    }

    function insert_id()
    {
        if((!$this->connection) || (!$this->query[$this->query_no]))
            $this->error("Unknown Query -record-!!!");

        return MySQL_insert_id();
    }
   
    function check($value, $quote="'") {
        if(!$this->connection)
            $this->sql_connect();

        $value = str_replace("'","&#39;",$value);
       
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }

        if(version_compare(PHPversion(),"4.3.0")=="-1") {
            $value = MySQL_escape_string($value);
        } else {
            $value = MySQL_real_escape_string($value);
        }
        $value = $quote . $value . $quote;
    
        return $value;
    }
   
    function error($error_message)
    {
        if($this->debug == 1)
            echo "Fatal Error: ".$error_message;
        exit();
    }
}
?>

Now lets se how to use it.

at first we need to include this PHP db class file

include('db.PHP');

Then we need to declare user class;

$db = new database();

After that, we can start to use our MySQL database class. At first I want to remind you about debug variable. if you work on local server, type it to 1 to be able to see error message. If you work on server, type it to 0 because othervwise any other user can see your passible database error messages and use against you.

At first we need to send query :

$db->query("SELECT * FROM user WHERE user = ".$db->check($user)." AND password = ".$db->check($password)."");

I use $db->check() to control input value for unwanted entries. It is just for security and protect your database against illegal attempts. BE SURE TO USE THIS FOR EACH INPUT VALIABLE.

After sending query, we need to get data:

$result = $db->fetch_array();

of if you get more than one row:

while($result = $db->fetch_array()) { }

it will bring you an array and we can show it like;

echo $result['user'];

we use database field names to show fetched data.

if you want to get number of rows;

$totalRow = $db->num_rows();

For update, delete and insert queries, you just need to send them to server.

$db->query("INSERT INTO user VALUES(".$db->check($user).",".$db->check($password).")");

if you want to get last transactions insert ID, you can use:

$userID = $db->insert_id();

it will bring you the inserted data auto-increase primary key value generated automatically by server.

As you see, it is so simple