Samsung S5233 with 3 MP Camera, MP3 player
Apple iPod nano 8 GB Mavi
Samsung SL30 10MP Digital Camera with 3x Optical Zoom and 2.5 inch LCD
Apple iPod shuffle 4 GB Mavi
:: 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 :
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)

    function dbname($databasename)

    function user($dbuser,$userpwd)

    function sql_connect()
            $this->error("Unknown Database");


            $this->error("Connection Problem");

            $this->error("Unknown Database");

    function sql_close()

    function query($sqlquery)

        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="'") {

        $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;

Now lets se how to use it.

at first we need to include this PHP db class file


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