views:

37

answers:

1

I have a database:

   class dbConnect
{
    var $strHost     = "";
    var $strDatabase = "";
    var $strUser     = "";
    var $strPassword = "";

var $intLinkID  = 0;
var $intQueryID = 0;
var $arrRecord  = array();
var $intRow;

var $intErrno = 0;
var $strError = "";

function dbConnect()
{
    $this->Connect();
}

function dbHalt($strError)
{
    $objError = new errorHandle();

    $objError->reportError("Database error: $strError", false);
    $this->intErrno = mysql_errno($this->intLinkID);
    $this->strError = mysql_error($this->intLinkID);

    $strError = sprintf("MySQL Error: %s (%s)", $this->intErrno, $this->strError);

    $objError->reportError($strError, true);
}

function Connect()
{
    if (0 == $this->intLinkID)
    {

            $this->intLinkID=mysql_connect($this->strHost, $this->strUser, $this->strPassword);
            if (!mysql_select_db($this->strDatabase, $this->intLinkID))
            {
                $this->dbHalt("cannot use database ". $this->strDatabase);
            }

    }

    if (!$this->intLinkID)
    {
        $this->dbHalt("Database connection failed");
    }


}
    class dbMain extends dbConnect
{
    // Database connection settings.

    // Dev server.
    var $strHost = 'db_host_here';
    var $strUser = 'db_User_here';
    var $strPassword = 'db_password_here';
    var $strDatabase = 'db_name_here';


}

This works fine when calling $objDB = new dbMain();

But what I want is to dynamically set the database details in another class (for a master CMS) eg:

$objDB = new dbMain($user, $pass, $dbname, $dbhost);
A: 

Use a class constructor. Pass in values.

class dbMain 
{
   function __construct($strHost='db_host_here',$strUser='db_User_here',$strPassword='db_password_here',$strDatabase='db_name_here'){
        $this->strHost = $strHost;
        $this->strUser = $strUser;
        $this->strPassword = $strPassword;
        $this->strDatabase = $strDatabase;
    }
}
Stewie
Can you give example?
class dbDynamic extends dbConnect { function __construct($strHost='localhost',$strUser='user',$strPassword='password',$strDatabase='database'){ $this->strHost = $strHost; $this->strUser = $strUser; $this->strPassword = $strPassword; $this->strDatabase = $strDatabase; }}
The above brings errors about mysql_connect not valid link resource?
check your parameters. Check error during mysql_connect(); with mysql_error()
Stewie