Re: Multiple instances of mysql_connect() in single PHP document.

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

 



2009/11/11 Bastien Koert <phpster@xxxxxxxxx>:
> On Tue, Nov 10, 2009 at 4:14 PM, Timothy Legg <php_list@xxxxxxxxxxxxxxx> wrote:
>>
>> Hello,
>>
>> Is it wrong to have mysql_connect() called twice within a single PHP
>> document, such as this?  I have been running into some fairly annoying
>> query failures that commute when I change the order of these two blocks of
>> code.  I am currently working around this by repeatedly using
>> mysql_select_db() to change the databases as I am needing to change to a
>> different database.
>>
>> To be more specific, I can use the $db_material handle in doing a query,
>> but it will always try to search in the Labor_Log table and thus fail
>> because the tables have different and names.  Swapping these blocks will
>> have a vice-versa effect.
>>
>>
>>        //Connect to server (materials)
>>        $db_material = mysql_connect("localhost", "root", "secret");
>>        if (!$db_material)
>>        {
>>                echo( "<P>Unable to connect to the database server at this
>> time.</P>" );
>>                exit();
>>        }
>>        mysql_select_db("Material_Log", $db_material);
>>
>>
>>        //Connect to server (labor)
>>        $db_labor = mysql_connect("localhost", "root", "secret");
>>        if (!$db_labor)
>>        {
>>                echo( "<P>Unable to connect to the database server at this
>> time.</P>" );
>>                exit();
>>        }
>>        mysql_select_db("Labor_Log", $db_labor);
>>
>>
>>
>> Tim Legg
>>
>>
>> --
>> PHP Database Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> If the databases are in the same mysql server, then you could qualify
> the table select with the database name and simply re-use the
> connection
>
> select db_name.table_name.field from db_name.table_name [where]
>
>
> --
>
> Bastien
>
> Cat, the other other white meat
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

As I understand things, if you make 2 or more connections to the same
database server with the same credentials, you end up with 1 shared
connection.

This is assuming you are not using mysql_pconnect() and you are not
using the new_link option on mysql_connect().

See http://svn.php.net/viewvc/php/php-src/trunk/ext/mysql/php_mysql.c?view=markup#l864.

If you then use change DB on one of the connection, you are changing
it for both.

The answer is to use a single connection and a fully qualified name to
the table.

SELECT Alias.Column FROM Database.Table.Column AS Alias

sort of thing.



-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
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