Re: Multiple instances of mysql_connect() in single PHP document.
| From: | Chris | Date: | Tue, 10 Nov 2009 22:20:30 +0000 |
| Subject: | Re: Multiple instances of mysql_connect() in single PHP document. | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-46641@lists.php.net to get a copy of this message | ||
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: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///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 1<<//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"?CASE 2<<