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
https://forums.phpfreaks.com/topic/149793-solved-mysql-class/
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
https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-786599
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
https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-786622
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
https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-788308
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.