Re: Connecting to two MySQL databases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well, the class creation I screwed up on, but I think you should get the idea 
anyhow. 
-Micah 

On Thursday 29 September 2005 11:10 am, Micah Stevens wrote:
> I don't see a reason to duplicate the class code, just make it keep track
> of the $link variable, and each instance of the class can be a seperate
> connection.. Then you can do what you want:
>
> $db1 = new dbconn;
> $db2 = new dbconn;
>
> $db1->dbconn($host, $user, $pw);
> $db2->dbconn($host2, $user2, $pw2);
>
> $result_from_first_db = $db1->retrieveData($sql);
> $result_from_second_db = $db2->retrieveData($sql);
>
>
> You're making the class too specific. IMHO.
> -Micah
>
> On Thursday 29 September 2005 10:56 am, Charles Kline wrote:
> > Hi all,
> >
> > I am working on an application that requires me to connect to two
> > MySQL databases both hosted on the same server.
> >
> > I have an existing set of class files I created to handle the db
> > connection to the main database. To connect to the second database, I
> > duplicated my db class files and renamed them, and also renamed the
> > functions etc. but what is happening, is that the calls made to the
> > second database connection seem to get made against the first
> > database. So that I get errors like: dbname.tablename was not found
> > (or something like that).
> >
> > What is the best practice for this? I need to have two open db
> > connections for just two little things in my application.
> >
> > My class files are one makes the connection and the other calls the
> > various get and set functions to the db.
> >
> > Here is what I have for the dbconnect.php class:
> >
> > class dbconn {
> >    // database setup.  These should be changed accordingly.
> >    var $dbpath = "localhost";
> >    var $dbname = "mydb";
> >    var $dblogin = "test";
> >    var $dbpass = "test";
> >
> >
> >    function dbconn() {
> >      $link = mysql_connect($this->dbpath, $this->dblogin, $this-
> >
> >  >dbpass) or die ('Not Connected: ' . mysql_error());
> >
> >      mysql_select_db($this->dbname, $link) or die ('Can\'t use this
> > database: ' . mysql_error());
> >
> >    }
> >
> >    function retrieveData( $sql ) {
> >      $rs = mysql_query( $sql ) or die("Invalid query: " . mysql_error
> > ());
> >      // if no result, return null
> >      if (($rs == null) || (mysql_num_rows($rs) == 0)) {
> >        return null;
> >      } else {
> >        return ( $rs );
> >      }
> >    }
> >
> >    function insertData( $sql ) {
> >      mysql_query( $sql );
> >      // return new complaint id if insert is successful
> >      if ( mysql_affected_rows() > 0 ) {
> >        return ( mysql_insert_id() );
> >      } else {
> >        return null;
> >      }
> >    }
> >
> >    function updateData( $sql ) {
> >      $rs = mysql_query( $sql );
> >      if (mysql_affected_rows() > 0) {
> >        return ( $rs );
> >      } else {
> >        return null;  // no changes were made
> >      }
> >    }
> > }
> >
> >
> > I posted this to the php-general list the other day, but never got it
> > working right with the suggestions. Sorry if this is a repeat for
> > anyone.
> >
> > - Charles
> >
> > --
> > RightCode, Inc.
> > 900 Briggs Road #130
> > Mount Laurel, NJ 08054
> > P: 856.608.7908
> > F: 856.439.0154
> > E: ckline@xxxxxxxxxxxxx

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux