RE: [PHP-DB] Connecting to two MySQL databases
| From: | Miguel Guirao | Date: | Thu, 29 Sep 2005 18:22:59 +0000 |
| Subject: | RE: [PHP-DB] Connecting to two MySQL databases | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-40414@lists.php.net to get a copy of this message | ||
I agree with this solution!!
-----Original Message-----
From: Micah Stevens [mailto:micah@raincross-tech.com]
Sent: Jueves, 29 de Septiembre de 2005 01:10 p.m.
To: php-db@lists.php.net
Subject: Re: [PHP-DB] Connecting to two MySQL databases
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@rightcode.net
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Este mensaje es exclusivamente para el uso de la persona o entidad a quien esta dirigido; contiene
informacion estrictamente confidencial y legalmente protegida, cuya divulgacion es sancionada por la
ley. Si el lector de este mensaje no es a quien esta dirigido, ni se trata del empleado o agente
responsable de esta informacion, se le notifica por medio del presente, que su reproduccion y
distribucion, esta estrictamente prohibida. Si Usted recibio este comunicado por error, favor de
notificarlo inmediatamente al remitente y destruir el mensaje. Todas las opiniones contenidas en
este mail son propias del autor del mensaje y no necesariamente coinciden con las de Radiomovil
Dipsa, S.A. de C.V. o alguna de sus empresas controladas, controladoras, afiliadas y subsidiarias.
Este mensaje intencionalmente no contiene acentos.
This message is for the sole use of the person or entity to whom it is being sent. Therefore, it
contains strictly confidential and legally protected material whose disclosure is subject to penalty
by law. If the person reading this message is not the one to whom it is being sent and/or is not an
employee or the responsible agent for this information, this person is herein notified that any
unauthorized dissemination, distribution or copying of the materials included in this facsimile is
strictly prohibited. If you received this document by mistake please notify immediately to the
subscriber and destroy the message. Any opinions contained in this e-mail are those of the author of
the message and do not necessarily coincide with those of Radiomovil Dipsa, S.A. de C.V. or any of
its control, controlled, affiliates and subsidiaries companies. No part of this message or
attachments may be used or reproduced in any manner whatsoever.