Jump to content

Test MySql Connection


flaab

Recommended Posts

I don't see why you can't catch any errors thrown by the mysql_connect call and use those to determine whether the connection is valid. Something along the lines of:

 

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link)
{
    echo 'The connection is not working.';
}
else
{
   echo 'The connection is working ';
   mysql_close($link);
}

 

Furthermore, you could wrap the whole thing in a try/catch block just to be extra sure that an exception will not screw things up for you.

 

Regards,

Darren.

Link to comment
https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342016
Share on other sites

Hi thanks for answering

 

I've tried both ways, using a try catch block and using the value of $link to determine wether the connection could be created...

 

The thing is...yeah, that is what i was looking for. But, when the connection fails Php throws a bunch of warnings i do not want to appear.

 

Warning: mysql_pconnect() [function.mysql-pconnect]: Access denied for user 'ComplaintMater'@'localhost' (using password: YES) in /var/www/ArasPhp/core/libs/Core.php on line 57

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/ArasPhp/core/libs/Core.php on line 58

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /var/www/ArasPhp/core/libs/Core.php on line 58

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /var/www/ArasPhp/core/libs/Core.php:57) in /var/www/ArasPhp/core/helpers/Session.php on line 16

Warning: mysql_pconnect() [function.mysql-pconnect]: Access denied for user 'ComplaintMater'@'localhost' (using password: YES) in /var/www/ArasPhp/core/libs/Core.php on line 37

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/ArasPhp/core/libs/Core.php on line 38

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /var/www/ArasPhp/core/libs/Core.php on line 38

 

I would like to test the connection without letting php throw all that...like a silent way and just getting a boolean value.

 

Ideas? Thanks!

Link to comment
https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342034
Share on other sites

You can use the 'error-suppression operator' (the @ symbol) as follows:

 

$link = @mysql_connect('localhost', 'mysql_user', 'mysql_password');

 

This is mentioned in the FAQ: http://www.phpfreaks.com/forums/index.php/topic,95563.0.html

 

Haven't tried it myself but sounds plausible.

 

Regards,

Darren.

Link to comment
https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342218
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.