Multiple MySQL pointers

Eric Jensen eric at emstraffic.com
Wed Feb 2 10:27:09 MST 2005


A co-worker and I did some good old fashion RTFM on some of comments on 
php.net and found the problem, thought it was good to know and so I am 
sharing.

If you do not change any paramaters in your later mysql_connect calls 
then it will still use the first connection.  Which is why it worked 
again for me when I changed the login information.  You can also specify 
a port if you need to use the same login info.  As of PHP 4.2.0 they 
added another field to mysql_connect that allows you to create new 
connections regardless.

mysql_connect("localhost", "user", "password", new_link);

So take a mental note before you go around truncating all the tables in 
the wrong database..... *sigh* ;)

Eric

Eric Jensen wrote:

> Had a huge database problem yesterday, we are trying to merge two 
> databases that are nearly identical.  Here is how I have my pointers 
> setup:
>
> //connects to new merge-ready database
> $db = mysql_connect("localhost", "user1", "********") or 
> die(mysql_error());
> mysql_select_db("db_one", $db) or die(mysql_error($db));
>
> //connects to first database
> $db_sm = mysql_connect('localhost', 'user1', '*******') or 
> die(mysql_error());
> mysql_select_db("db_two", $db_sm) or die(mysql_error($db_sm));
>
> //connect to second database
> $db_ems = mysql_connect('outside_host', 'user2', '******') or 
> die(mysql_error());
> mysql_select_db("db_two", $db_ems) or die(mysql_error($db_ems));
>
> Problem we ran into was even though all the MySQL commands were using 
> specific pointers (I.E. mysql_query($sql, $db)) the first pointer's 
> ($db) database was changed from "db_one" to "db_two".  Later with some 
> experimenting it turns out that having the same login is what caused 
> that.  As soon as I changed the first pointer to a different user it 
> worked as intended:
>
> //connects to new merge-ready database
> $db = mysql_connect("localhost", "new_user", "********") or 
> die(mysql_error());
> mysql_select_db("db_one", $db) or die(mysql_error($db));
>
> Anybody heard of this problem or can explain why?  But more 
> importantly for now, anybody know a MySQL command that will allow me 
> to double check what database I'm currently using?
>
> Eric
> .===================================.                                           
> | This has been a P.L.U.G. mailing. 
> |                                           |      Don't Fear the 
> Penguin.      |                                           |  IRC: 
> #utah at irc.freenode.net   
> |                                           
> `==================================='
>




More information about the PLUG mailing list