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

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

 



That is great for me to keep in mind for future reference. However, is
it ever appropriate to connect to the database twice in a single script.
Two ways I can think of doing this would be:
>>CASE 1<<

//Connect to server (materials)
$db_material = mysql_connect("localhost", "root", "secret");

Some code....

//Connect to server (materials)
$db_material = mysql_connect("localhost", "root", "secret");

>>CASE 2<<

//Connect to server (materials)
$db_material = mysql_connect("localhost", "root", "secret");

...Code to close connection here ....

Some code...

//Reconnect to server
//Connect to server (materials)
$db_material = mysql_connect("localhost", "root", "secret");

I don't see why you would use the first case, but would it cause issues
if you did?

In the second example, the connection was closed in part of a script and
connect to the database again comes later in the script.

Is this "allowable"?

-----Original Message-----
From: Chris [mailto:dmagick@xxxxxxxxx] 
Sent: Tuesday, November 10, 2009 4:51 PM
To: php_list@xxxxxxxxxxxxxxx
Cc: php-db@xxxxxxxxxxxxx
Subject: Re:  Multiple instances of mysql_connect() in single
PHP document.

Timothy Legg 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.

I assume you mean "database" and not "table". A database can contain 
many tables and there is no issue with querying multiple tables in a 
single database.

There is no issue with connecting to multiple databases in the one
script.


> 
>         //Connect to server (materials)
>         $db_material = mysql_connect("localhost", "root", "secret");

>         //Connect to server (labor)
>         $db_labor = mysql_connect("localhost", "root", "secret");

So now when you use mysql_query you should pass in the right link to
use:

$result = mysql_query($sql, $db_material);

or

$result = mysql_query($sql, $db_labor);


-- 
Postgresql & php tutorials
http://www.designmagick.com/


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Confidentiality Notice: 
This e-mail and any attachments may contain confidential information intended solely for the use of the addressee.  If the reader of this message is not the intended recipient, any distribution, copying, or use of this e-mail or its attachments is prohibited.  If you received this message in error, please notify the sender immediately by e-mail and delete this message and any copies. Thank you.

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