Page 1 of 1

Class to connect to the db

Posted: Sun Aug 08, 2004 11:43 pm
by System_Failure

Code: Select all

class DB {
        function DB() {
            $this->host = "localhost";
            $this->db = "database";
            $this->user = "root";
            $this->pass = "pass";
            $this->link = mysql_connect($this->host, $this->user, $this->pass);
            mysql_select_db($this->db);
            register_shutdown_function(array(&$this, 'close'));
        }
        function close() {
            mysql_close($this->link);
        }
    }
I use that class to connect to my database and use "$DB->link" beside my query statement.

Now, with that I can only connect to one database, how would I connect to another at the same time using the same type of method? I dont have enough knowledge at this to do this on my own :(

Thanks.

Posted: Mon Aug 09, 2004 1:32 am
by timvw
Pass host, user, pass and database as parameters to the constructor would be a solution.

Posted: Mon Aug 09, 2004 11:25 am
by System_Failure
and how do I do that?

Posted: Mon Aug 09, 2004 11:32 am
by marker5a

Code: Select all

<?php
function DB($pass,$user,$db) { 
            $this->host = "localhost"; 
            $this->db = "$db"; 
            $this->user = "$user"; 
            $this->pass = "$pass"; 
            $this->link = mysql_connect($this->host, $this->user, $this->pass); 
            mysql_select_db($this->db); 
            register_shutdown_function(array(&$this, 'close')); 
        } 
        

?>
I think that should do it, but dont count on it 100 %

Posted: Mon Aug 09, 2004 11:33 am
by feyd

Code: Select all

new DB('host','user','pass','database');

// .........

function DB($host,$user,$pass,$db)
{
  $this->host = $host;
  // .........
}

Posted: Mon Aug 09, 2004 11:35 am
by System_Failure
The whole purpose of using the class was to enter the database info only once in the class :(....

With your methods, I will have to pass data to the class everytime! :oops:

Posted: Mon Aug 09, 2004 11:37 am
by feyd
that's exactly what that would do, for every instance of the DB class you create...

btw.. you can set default values for each of those..

Code: Select all

function DB($host = 'host', $user = 'user', $pass = 'pass', $db = 'db')
{
  //.......
}

Posted: Mon Aug 09, 2004 11:41 am
by System_Failure
feyd wrote:that's exactly what that would do, for every instance of the DB class you create...

btw.. you can set default values for each of those..

Code: Select all

function DB($host = 'host', $user = 'user', $pass = 'pass', $db = 'db')
{
  //.......
}
Hmm...I like the default value :p....thank you very much!

Posted: Mon Aug 09, 2004 11:58 am
by System_Failure
Now the default value is working just fine, so I am not having to pass values every time. However

Code: Select all

new DB('localhost','root','pass','database');
        $resultsu = mysql_query("SELECT * FROM user WHERE userid=$fuserid", $DB->link) or die(mysql_error());
I am getting a "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /path/functions.php"

and my whole db class is:

Code: Select all

class DB {
        function DB($host = 'localhost', $user = 'root', $pass = 'pass', $db = 'database') {
            $this->link = mysql_connect($host, $user, $pass);
            mysql_select_db($db);
            register_shutdown_function(array(&$this, 'close'));
        }
        function close() {
            mysql_close($this->link);
        }
    }
------------------------------------------

Edit: Seem to work after I take out the resource handler: " $DB->link"

Code: Select all

new DB('localhost','root','pass','database');
        $resultsu = mysql_query("SELECT * FROM user WHERE userid=$fuserid") or die(mysql_error());