dual_alliance Posted January 4, 2008 Share Posted January 4, 2008 Hello, While learning OOP l've hit a snag. I wish to be able to switch between two different database types mysqli and mysql. To do that l've setup the following... database.php mysql.database.php mysqli.database.php In database.php, it will eventually select from the config file what type of database to use. However, l still cant grasp the concept of how do achieve this. This is what l have so far. database.php include('mysql.database.php'); interface database { function connect($host, $db, $user, $pass) { $this->host = $host; $this->db = $db; $this->user = $user; $this->pass = $pass; } mysql.database.php class MySQL implements database { function connect() { mysql_connect($this-host,$this->user,$this->pass); mysql_select_db($this->db); echo 'working?'; } } I've changed the code many times from idea's l've found on the internet while trying to find out how to do this. I know the code above wont work properly, but l really would appreciate a kick in the right direction Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/84417-solved-switching-between-database-types/ Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 well you should initialize your username in the construct and not in the connect the connect function should be overidden some like the following interface database { function database($host, $db, $user, $pass) { $this->host = $host; $this->db = $db; $this->user = $user; $this->pass = $pass; } class MySQL implements database { function connect() { mysql_connect($this-host,$this->user,$this->pass); mysql_select_db($this->db); echo 'working?'; } } $objMysql = new MySQL("hostname","username","password","db"); $objMysql->connect(); Quote Link to comment https://forums.phpfreaks.com/topic/84417-solved-switching-between-database-types/#findComment-430025 Share on other sites More sharing options...
dual_alliance Posted January 4, 2008 Author Share Posted January 4, 2008 It's not exactly what l'm trying to acheive, as when it comes to a mysqli database that would be of no use as l want to use it in a way like this: $db = new database('w.e', 'w.e', 'w.e', w.e'); $db->query()... That way, if l switch to a server that has mysqli and no mysql all l have to do is edit the config file and the database.php will enable me to use the functions from mysqli.database.php (which l havent made yet) Thanks for trying to help Quote Link to comment https://forums.phpfreaks.com/topic/84417-solved-switching-between-database-types/#findComment-430036 Share on other sites More sharing options...
448191 Posted January 4, 2008 Share Posted January 4, 2008 This thread might help: http://www.phpfreaks.com/forums/index.php/topic,157069.0.html Then instead of: $db = DbCommon::factory('MySQL', 'localhost', 'root', 'pass'); do something like $db = DbCommon::factory(ApplicationConfig::get('DBType'), 'localhost', 'root', 'pass'); It is not advisable to place the dependency on the configuration inside the Database type. Quote Link to comment https://forums.phpfreaks.com/topic/84417-solved-switching-between-database-types/#findComment-430040 Share on other sites More sharing options...
dual_alliance Posted January 4, 2008 Author Share Posted January 4, 2008 Thanks 448191 thats exactly the kick in the right direction l needed! Quote Link to comment https://forums.phpfreaks.com/topic/84417-solved-switching-between-database-types/#findComment-430047 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.