Jump to content

[SOLVED] mysql class


HektoR

Recommended Posts

hi all.

i'm making mysql class . i made very simply code :

class DBS
{
var $result;
var $row;
function db_connect($host,$username,$password,$database)
{
	$con = mysql_connect($host,$username,$password);
	mysql_select_db($database,$con);	
}
function db_query($query)
{
	$this->result = mysql_query($query);
}
function db_fetch()
{
	$this->row = mysql_fetch_object($this->result);
}

}

 

but i think this is too bad code...

can anyone suggest idea how to edit this code or make new ??

 

 

Thank you

Link to comment
Share on other sites

Well, you are using PHP 4-style OO

 

class DBS
{
protected $result;
protected $row;
function db_connect($host,$username,$password,$database)
{
	$con = mysql_connect($host,$username,$password);
	mysql_select_db($database,$con);	
}
function db_query($query)
{
	$this->result = mysql_query($query);
}
function db_fetch()
{
	$this->row = mysql_fetch_object($this->result);
}

}

 

Link to comment
Share on other sites

If you want to create a db class, you should use the singleton design pattern for this.

 

example.

 

class DB{
     private static $instance = null;

     private function __construct(){
      //connection code.
    }

    public static function GetInstance(){

         if (! (self::$instance instanceof DB) ){
             self::$instance = new DB();
         }

         return self::$instance;
    }


   public function Query($sql){

          //do query
    }

    private function __clone(){

    }

   public function __destruct(){
     //clean resources. 
   }

}

 

usage

 


DB::GetInstance()->Query($sql);

 

I wrote this code of the top of my head. Not tested it.

Link to comment
Share on other sites

If you have php 5+ at your exposal why dont you simply use PDO?

 

PDO is slower in regards to response time; so unless you're building a system that is not exclusively for MySQL, then it's pretty much lost all of it's usefulness.

 

MySQLi would probably be a better choice.

Link to comment
Share on other sites

thank you guys for your replies.

 

with my code i had a while() problem

i couldn't get result with while cycle ...

and also what about if i want two query at one time ???

 

$result1 = $db->Query("SELECT * FROM etc");
$result2 = $db->Query("SELECT * FROM etc");

 

 

Link to comment
Share on other sites

thank you guys for your replies.

 

with my code i had a while() problem

i couldn't get result with while cycle ...

and also what about if i want two query at one time ???

 

$result1 = $db->Query("SELECT * FROM etc");
$result2 = $db->Query("SELECT * FROM etc");

 

oh yes :)

 

thank you very much all :)

 

SOLVED

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.