Entry
Resource identifier scope...
Jan 23rd, 2003 06:03
Greg Knapp, http://uk.php.net/manual/en/function.mysql-connect.php
OK so I'm answering my own question but I need more space to explain my
query (I also posted it 3 times by accident as this site is so slow).
On with the question:
"If a second call is made to mysql_connect() with the same arguments,
no new link will be established, but instead, the link identifier of
the already opened link will be returned"
Taken from: http://uk.php.net/manual/en/function.mysql-connect.php
Presumably the MySQL connection is closed an a new one created after
the first function call?
Example:
define('DBHOST', 'localhost');
define('DBUSER', 'php');
define('DBPSWD', 'itsasecret');
$DB = @mysql_connect(DBHOST, DBUSER, DBPSWD);
function foo_one()
{
$DB = @mysql_connect(DBHOST, DBUSER, DBPSWD);
... statements ...
}
function foo_two()
{
$DB = @mysql_connect(DBHOST, DBUSER, DBPSWD);
... statements ...
}
$barOne = foo_one();
$barTwo = foo_two();
Am I right in thinking that foo_one attempts to make a MySQL connection
but no new link is actually created (as one is already established
earlier in the script), and a pointer (to the existing connection) is
returned?
I know results are free'd when functions finish. What about (in this my
example above) $DB? Does this continue it's existance after foo_one()
is done?
If not then foo_two() does actually have to make a new connection,
instead of being handed a pointer like foo_one() was?
If this is the case then unnecessary closing and opening of MySQL
connections are being made. The best thing would be the global or pass
the resource identifier as an argument, check if it's false, then
establish a connection if it is false?