Jump to content

[SOLVED] Switching between database types


dual_alliance

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/84417-solved-switching-between-database-types/
Share on other sites

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();

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 :)

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.

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.