flaab Posted September 5, 2007 Share Posted September 5, 2007 Hi =) Is there any way to TEST wether a MySql connection is going to work? Without using mysql_connect that can throw an error? Just to display on the web..."The connection is working" or "The connection is not working" Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/68042-test-mysql-connection/ Share on other sites More sharing options...
recklessgeneral Posted September 5, 2007 Share Posted September 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342016 Share on other sites More sharing options...
flaab Posted September 5, 2007 Author Share Posted September 5, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342034 Share on other sites More sharing options...
recklessgeneral Posted September 5, 2007 Share Posted September 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342218 Share on other sites More sharing options...
cooldude832 Posted September 5, 2007 Share Posted September 5, 2007 if you are worried about public pages seeing it make a test page on your server and test it, otherwise I see no issues with displaying errors the only way to fix an error is seeing it. Quote Link to comment https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-342220 Share on other sites More sharing options...
flaab Posted September 6, 2007 Author Share Posted September 6, 2007 Wow thanks that was exactly was I was looking for!!!! Following this issue...Is there any way or operator to prevent a class method to be executed if not all required params are satisfied? Quote Link to comment https://forums.phpfreaks.com/topic/68042-test-mysql-connection/#findComment-343013 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.