Codeigniter/PHP 检查是否可以连接到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12483861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter/PHP check if can connect to database
提问by tim peterson
I'd like to backup my read replica(i.e., slave) database with my master database but this simple boolean I did failed:
我想用我的主数据库备份我的只读副本(即从属)数据库,但是我做的这个简单的布尔值失败了:
$config['hostname'] = "myReadReplicaDatabase.com";
//...$config['other_stuff']; other config stuff...
$db_obj=$CI->load->database($config, TRUE);
if(!$db_obj){
$config['hostname'] = "myMasterDatabase.com";
$db_obj=$CI->load->database($config, TRUE);
}
After terminating my read replica database I expected the boolean to evaluate to FALSEand the script to then use my master database. Unfortunately, instead I got the following PHP error:
终止我的只读副本数据库后,我希望布尔值评估为FALSE,然后脚本使用我的主数据库。不幸的是,我收到了以下 PHP 错误:
Unable to connect to your database server using the provided settings.
Filename: core/Loader.php
All i want is for the connection to return true or false, does anyone know how to do this in Codeigniter?
我想要的只是让连接返回 true 或 false,有人知道如何在 Codeigniter 中做到这一点吗?
回答by tim peterson
My question was answered on this thread on Codeigniter forums.
我的问题在 Codeigniter 论坛上的这个帖子中得到了回答。
The key is to notautoinitialize the database:
关键是不要自动初始化数据库:
$db['xxx']['autoinit'] = FALSE;
To suppress errors it you can set this
要抑制错误,您可以设置它
$db['xxx']['db_debug'] = FALSE;
Then in your code that checks the db state, check TRUE/FALSE of the initialize() function:
然后在检查数据库状态的代码中,检查 initialize() 函数的 TRUE/FALSE:
$db_obj = $this->database->load('xxx',TRUE);
$connected = $db_obj->initialize();
if (!$connected) {
$db_obj = $this->database->load('yyy',TRUE);
}
Here is my entire config file for future reference: https://gist.github.com/3749863.
这是我的整个配置文件以供将来参考:https: //gist.github.com/3749863。
回答by Iberê
Based on what was said here: Codeigniter switch to secondary database if primary is down
基于这里所说的: Codeigniter switch to secondary database if primary is down
You can check for the conn_idon the $db_obj
您可以conn_id在$db_obj
if ($db_obj->conn_id === false) {
$config['db_debug'] = true;
$config['hostname'] = "myMasterDatabase.com";
$db_obj=$CI->load->database($config, TRUE);
}
This should work.
这应该有效。
回答by Rajen K Bhagat
when you connect to database its returns connection object with connection id on successful condition otherwise return false.
当您连接到数据库时,它在成功条件下返回带有连接 ID 的连接对象,否则返回 false。
you can check it to make sure that database connection is done or not.
您可以检查它以确保数据库连接是否完成。
$db_obj=$CI->load->database($config, TRUE);
if($db_obj->conn_id) {
//do something
} else {
echo 'Unable to connect with database with given db details.';
}
try this and let me know, if you have any other issue.
试试这个,让我知道,如果你有任何其他问题。
回答by Sirius
I test all I found and nothing wokrs, the only way I found was with dbutil checking if database exists, something like this:
我测试了我发现的所有东西,没有任何工作,我发现的唯一方法是使用 dbutil 检查数据库是否存在,如下所示:
$this->load->database();
$this->load->dbutil();
// check connection details
if( !$this->dbutil->database_exists('myDatabase'))
echo 'Not connected to a database, or database not exists';
回答by wesside
try {
// do database connection
} catch (Exception $e) {
// DO whatever you want with the $e data, it has a default __toString() so just echo $e if you want errors or default it to connect another db, etc.
echo $e->getMessage();
// Connect to secondary DB.
}
For those who downvoted me, you can do this. Exception will catch PDOException.
对于那些拒绝我的人,你可以这样做。异常将捕获 PDOException。
try {
$pdo = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
mail('[email protected]', 'Database error message', $e->getMessage());
// and finally... attempt your second DB connection.
exit;
}
回答by karka91
$readReplica = @$CI->load->database($config, TRUE); // ommit the error
if ($readReplica->call_function('error') !== 0) {
// Failed to connect
}
Im not sure about the error code (not sure if its int/string) and don't have CI nearby to test this out but this principle should work
我不确定错误代码(不确定它的整数/字符串)并且附近没有 CI 来测试这个,但这个原则应该有效
回答by Omkesh Sajjanwar
$this->load->database();
print_r($this->db);

