HektoR Posted March 17, 2009 Share Posted March 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/ Share on other sites More sharing options...
Festy Posted March 17, 2009 Share Posted March 17, 2009 Can you please first tell us why do you think it's a bad code ? Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-786588 Share on other sites More sharing options...
waynew Posted March 17, 2009 Share Posted March 17, 2009 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-786599 Share on other sites More sharing options...
syed Posted March 17, 2009 Share Posted March 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-786622 Share on other sites More sharing options...
RichardRotterdam Posted March 17, 2009 Share Posted March 17, 2009 If you have php 5+ at your exposal why dont you simply use PDO? Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-786629 Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-787649 Share on other sites More sharing options...
Daniel0 Posted March 18, 2009 Share Posted March 18, 2009 If you want to create a db class, you should use the singleton design pattern for this. Not if he is aiming at good code, he should not. Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-787651 Share on other sites More sharing options...
waynew Posted March 18, 2009 Share Posted March 18, 2009 Multiple connections and all that??? Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-787658 Share on other sites More sharing options...
Daniel0 Posted March 18, 2009 Share Posted March 18, 2009 Yes. It might be required later on, and in that case you're screwed if you made it a singleton. Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-787661 Share on other sites More sharing options...
HektoR Posted March 19, 2009 Author Share Posted March 19, 2009 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-788301 Share on other sites More sharing options...
waynew Posted March 19, 2009 Share Posted March 19, 2009 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"); Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-788306 Share on other sites More sharing options...
HektoR Posted March 19, 2009 Author Share Posted March 19, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149793-solved-mysql-class/#findComment-788308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.