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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.