Re: [PHP] Two MySQL connections in one script not working as expected
If you make two calls to mysql_connect with the same parameters, the second will not make a new connection - it will return the existing connection. The manual explains this: http://www.php.net/mysql_connect If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. The new_link parameter modifies this behavior and makes mysql_connect() always open a new link, even if mysql_connect() was called before with the same parameters. -robin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Two MySQL connections in one script not working as expected
Hi all, I have a script that needs to update data in two databases. My db connections are both in class files that I created to execute the various connections and queries. What is happening is that the second database connection does not seem to work. The error I get is that it seems the second query is being executed against the first database connection - does that make sense? So I get an error that the database_name.table_name does not exist, which is true, but the query is getting executed against the wrong database name. Any ideas? Thanks, Charles -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Two MySQL connections in one script not working as expected
- Original Message - From: Charles Kline [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Tuesday, September 27, 2005 8:25 PM Subject: [PHP] Two MySQL connections in one script not working as expected Hi all, I have a script that needs to update data in two databases. My db connections are both in class files that I created to execute the various connections and queries. What is happening is that the second database connection does not seem to work. The error I get is that it seems the second query is being executed against the first database connection - does that make sense? So I get an error that the database_name.table_name does not exist, which is true, but the query is getting executed against the wrong database name. Any ideas? Thanks, Charles -- Hi there! Maybe you are using the same variables that may confuse things...? /G http://www.varupiraten.se/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Two MySQL connections in one script not working as expected
Hi Without you actually showing us these class files we can only guess but a common mistake is this: mysql_open(connection details) mysql_query(query) In those cases the last opened handle is used. To prevent this, use this syntax $db1=mysql_open(connection for db1); $db2=mysql_open(connection for db2); mysql_query($db1,$query_db_1); mysql_query($db2,$query_db_2); If you have used this syntax then check your class if it is using a global variable to hold the database handle and if it does make it a class variable instead Instead of $db=null class db {function db() {$GLOBALS['db']=mysql_open(... do instead class db {var $db=null; function db() {$this-db=mysql_open That way you can instanciate as many instances of the class as you like and each will have its own database handle. HTH Andy On Tuesday 27 September 2005 20:25, Charles Kline wrote: Hi all, I have a script that needs to update data in two databases. My db connections are both in class files that I created to execute the various connections and queries. What is happening is that the second database connection does not seem to work. The error I get is that it seems the second query is being executed against the first database connection - does that make sense? So I get an error that the database_name.table_name does not exist, which is true, but the query is getting executed against the wrong database name. Any ideas? Thanks, Charles -- Registered Linux User Number 379093 Now listening to Radio Stream amaroK::the Coolest Media Player in the known Universe! Cockroaches and socialites are the only things that can stay up all night and eat anything. Herb Caen -- -- --BEGIN GEEK CODE BLOCK- Version: 3.1 GAT/O/E$ d-(---)+ s:(+): a--(-)? C$(+++) UL$ P-(+)++ L+++$ E---(-)@ W++$ !N@ o? !K? W--(---) !O !M- V-- PS++(+++) PE--(-) Y+ PGP++(+++) t+(++) 5-- X++ R*(+)@ !tv b-() DI(+) D+(+++) G(+) e$@ h++(*) r--++ y--() -- ---END GEEK CODE BLOCK-- -- Check out these few php utilities that I released under the GPL2 and that are meant for use with a php cli binary: http://www.vlaamse-kern.com/sas/ -- pgph3H1sUsSCY.pgp Description: PGP signature
Re: [PHP] Two MySQL connections in one script not working as expected
On Sep 27, 2005, at 3:42 PM, Andy Pieters wrote: Hi Without you actually showing us these class files we can only guess but a common mistake is this: mysql_open(connection details) mysql_query(query) In those cases the last opened handle is used. To prevent this, use this syntax $db1=mysql_open(connection for db1); $db2=mysql_open(connection for db2); mysql_query($db1,$query_db_1); mysql_query($db2,$query_db_2); If you have used this syntax then check your class if it is using a global variable to hold the database handle and if it does make it a class variable instead Instead of $db=null class db {function db() {$GLOBALS['db']=mysql_open(... do instead class db {var $db=null; function db() {$this-db=mysql_open That way you can instanciate as many instances of the class as you like and each will have its own database handle. HTH Andy What I have is more like this: class db { var $dbpath = localhost; var $dbname = testdb; var $dblogin = test; var $dbpass = test; function db() { $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 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 then have another class with a different name and I changed the names of the functions as well. I have another set of class files that contain my various queries etc. Thanks for any help. Charles -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Two MySQL connections in one script not working as expected
Charles Kline wrote: On Sep 27, 2005, at 3:42 PM, Andy Pieters wrote: Hi Without you actually showing us these class files we can only guess but a common mistake is this: mysql_open(connection details) mysql_query(query) In those cases the last opened handle is used. To prevent this, use this syntax $db1=mysql_open(connection for db1); $db2=mysql_open(connection for db2); mysql_query($db1,$query_db_1); mysql_query($db2,$query_db_2); If you have used this syntax then check your class if it is using a global variable to hold the database handle and if it does make it a class variable instead Instead of $db=null class db {function db() {$GLOBALS['db']=mysql_open(... do instead class db {var $db=null; function db() {$this-db=mysql_open That way you can instanciate as many instances of the class as you like and each will have its own database handle. HTH Andy What I have is more like this: class db { var $dbpath = localhost; var $dbname = testdb; var $dblogin = test; var $dbpass = test; function db() { $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 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 then have another class with a different name and I changed the names of the functions as well. I have another set of class files that contain my various queries etc. Thanks for any help. Charles that's where it's going wrong. You're not linking db::db()'s link in the db::insertData() mysql_query() function. Thus, the mysql_query function defaults to the last opened mysql connection regardless of object. A way to fix this is to store the link in $this-link instead, and changing your mysql_query()'s to mysql_query($sql, $this-link); same goes for your mysql_affected_rows() and mysql_insert_id() functions -tul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Two MySQL connections in one script not working as expected
On Sep 27, 2005, at 5:23 PM, M. Sokolewicz wrote: Charles Kline wrote: On Sep 27, 2005, at 3:42 PM, Andy Pieters wrote: Hi Without you actually showing us these class files we can only guess but a common mistake is this: mysql_open(connection details) mysql_query(query) In those cases the last opened handle is used. To prevent this, use this syntax $db1=mysql_open(connection for db1); $db2=mysql_open(connection for db2); mysql_query($db1,$query_db_1); mysql_query($db2,$query_db_2); If you have used this syntax then check your class if it is using a global variable to hold the database handle and if it does make it a class variable instead Instead of $db=null class db {function db() {$GLOBALS['db']=mysql_open(... do instead class db {var $db=null; function db() {$this-db=mysql_open That way you can instanciate as many instances of the class as you like and each will have its own database handle. HTH Andy What I have is more like this: class db { var $dbpath = localhost; var $dbname = testdb; var $dblogin = test; var $dbpass = test; function db() { $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 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 then have another class with a different name and I changed the names of the functions as well. I have another set of class files that contain my various queries etc. Thanks for any help. Charles that's where it's going wrong. You're not linking db::db()'s link in the db::insertData() mysql_query() function. Thus, the mysql_query function defaults to the last opened mysql connection regardless of object. A way to fix this is to store the link in $this-link instead, and changing your mysql_query()'s to mysql_query($sql, $this-link); same goes for your mysql_affected_rows() and mysql_insert_id() functions -tul Hmm... still getting the same results. Here is my modified class files: class db { // database setup. These should be changed accordingly. var $dbpath = localhost; var $dbname = blah1; var $dblogin = test; var $dbpass = test; function db() { $this-link = mysql_connect($this-dbpath, $this-dblogin, $this- dbpass) or die ('Not Connected: ' . mysql_error()); mysql_select_db($this-dbname, $this-link) or die ('Can\'t use this database: ' . mysql_error()); } function retrieveData( $sql ) { $rs = mysql_query( $sql,$this-link ) 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,$this-link ); // return new complaint id if insert is successful if ( mysql_affected_rows($this-link) 0 ) { return ( mysql_insert_id($this-link) ); } else { return null; } } function updateData( $sql ) { $rs = mysql_query( $sql,$this-link ); if (mysql_affected_rows($this-link) 0) { return ( $rs ); } else { return null; // no changes were made } } } and stored in another file... class db2 { // database setup. These should be changed accordingly. var $dbpath = localhost; var $dbname = blah2; var $dblogin = test; var $dbpass = test; function dbzen() { $this-link = mysql_pconnect($this-dbpath, $this-dblogin, $this-dbpass) or die ('Not Connected: ' . mysql_error()); mysql_select_db($this-dbname, $this-link) or die ('Can\'t use this database: ' . mysql_error()); } function retrieveZenData( $sql ) { $rs = mysql_query( $sql,$this-link ) 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 insertZenData( $sql ) { mysql_query( $sql,$this-link ) or die(Invalid query: . mysql_error()); // return new complaint id if insert is successful if ( mysql_affected_rows($this-link) 0 ) { return ( mysql_insert_id($this-link) ); } else { return null; } } function updateZenData( $sql ) { $rs = mysql_query( $sql,$this-link );