views:

65

answers:

1

I'm just about finished my first release of automailer, a program I've been working on for a while now. I've just got to finish writing the installer. Its job is to rewrite the codigniter configs from templates. I've got the read/write stuff working, but I'd like to be able to test the server credentials given by the user without codingiter throwing a system error if they're wrong. Is there a function other than mysql_connect that I can use to test a connection that will return true or false and won't make codeigniter have a fit?

This is what I have

function _test_connection(){
    if(mysql_connect($_POST['host'], $_POST['username'], $_POST['password'], TRUE))
        return TRUE;
    else
        return FALSE;
}

Codigniter doesn't like this and throws a system error.

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

    <h4>A PHP Error was encountered</h4>

    <p>Severity: Warning</p>
    <p>Message:  mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Unknown MySQL server host 'x' (1)</p>
    <p>Filename: controllers/install.php</p>
    <p>Line Number: 57</p>

</div>

I'd rather not turn off error reporting.

+1  A: 

You can use the @ operator to suppress error to be thrown by PHP.

function _test_connection(){
    $db = @mysql_connect($_POST['host'], $_POST['username'], $_POST['password'], TRUE);
    if($db)
        return TRUE;
    else
        return FALSE;
}

Note that most of the time it is not good idea to use it as it will hide any errors even critical.

Check out Error Handling and Logging sections on PHP manual to learn more about.

Boris Guéry
Very cool, thankyou
Robert Hurst