Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.