2

Imagine the simple snippet below:

<?
mysql_close();
?>

This will obviously output the following warning (may look different depending on what version of php_mysql(i) you are using):

mysql_close(): no MySQL-Link resource supplied in .....

As there is no link open.

Is there a way in php to test if a mysql link is already established or not?

Thanks!


Note, have been getting a few comments regarding the use of mysql_close(). I just used that as an example. Furthermore, there are (many) situations when the use of mysql_close() is appropriate.

anonymous-one
  • 14,454
  • 18
  • 60
  • 84

3 Answers3

2

If you keep track of the resource identifier, you can use if ($link) which will get destroyed if you call mysql_close and then it will no longer evaluate true.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • Yea I thought about that, but as it stands now, we do not track the identifier. Will look into implementing this. – anonymous-one Sep 22 '11 at 05:42
  • So far this looks like the best way of doing this without doing something dirty like if(!@mysql_close()){ echo 'close failed, assume no connection...'; } – anonymous-one Sep 22 '11 at 06:09
0

well i'm not sure but see the answer here

Is closing the mysql connection important?

From the documentation:

Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().

Community
  • 1
  • 1
Eli Y
  • 877
  • 16
  • 41
0

If you need reconnect to mysql, look at this function: http://www.php.net/manual/ru/function.mysql-ping.php

classic
  • 544
  • 2
  • 7