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 rathe开发者_运维百科r not turn off error reporting.
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.
This isn't the best solution as it uses php's built-in mysql connect function. The following DOESNT WORK! however this is how codeigniter should work to test a users database input in the installation of an app.
$config['hostname'] = "localhost";
$config['username'] = "username";
$config['password'] = "password";
$config['database'] = "database_name";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = FALSE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
if($this->load->database($config)==TRUE){
return TRUE;
}else{
return FALSE;
}
精彩评论