You are here: Re: Multiple db access from php « PHP Programming Language « IT news, forums, messages
Re: Multiple db access from php

Posted by Gordon Burditt on 11/12/06 00:48

> I'm having problems attempting to access two databases from the same php
>script. Both databases are on the same server. Basically, I need to open
>db1, then open db2, close db2 and then return to using db1 as the default. I

Don't use a database "as a default". Use it explicitly: pass the
appropriate connection handle to mysql_query().

>thought something along the lines of the following would work but it
>doesn't:
>
>// connect to DB1
>$db1c = mysql_connect (DB1_HOST, DB1_USER, DB1_PASSWORD);
>mysql_select_db (DB1_NAME, $db1c)
>
>// use DB1

Pass $db1c to mysql_query() here.

>// now connect to DB2
>$db2c = mysql_connect (DB2_HOST, DB2_USER, DB2_PASSWORD);
>mysql_select_db (DB2_NAME, $db2c)
>
>// use DB2

Pass $db2c to myssql_query() here.

>// now return to using DB1
>mysql_close ($db2c);
>mysql_select_db (DB1_NAME, $db1c);

Why is this mysql_select_db needed here?

>// use DB1

Pass $db1c to mysql_query() here.

>// THE END
>
>However, what I see is that when returning to use DB1, I get access denied
>warnings suggesting that the system is using the username and password of
>DB2 with DB1.

Try issuing queries like "SELECT user()" and "SELECT database()" to
be sure.

>Also, one thing I have noticed is that the using echo() to render the values
>of $db1c and $db2c to the screen gives the same result which seems odd to
>me.

Something's wrong here. Did you do that before or after closing $db1c?

If you were using mysql_pconnect(), and the values for hostname, user,
and password happened to be the same for both (but different databases),
it might use the same connection for both.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация