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

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

 



Nehemias Duarte wrote:
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"?

It's allowable - whether it's the right thing or not is another question :)

Ideally you'd only connect to a particular database once (the original example was connecting to two different databases).

If you don't close the connection explicitly, php will do it at the end of the script as part of it's internal 'cleanup' processing - so you don't need to have a mysql_close() at the end of the script. This is good because in some complicated scripts (or cms'es) it'd be pretty hard to work out where the script "ends" and make sure there is a db close call in all the right spots.

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


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