有没有一种方法,使CI抛出异常当它遇到一个DB错误而不是显示这样的消息的:CodeIgniter - 如何捕获数据库错误?
数据库出错错误编号:1054 未知列“富“在 'where子句' SELECT * FROM(
FooBar
)WHEREfoo
= '1'
注:我只希望这在一个控制器发生。在其他控制器中,我很高兴能够显示DB错误消息。
有没有一种方法,使CI抛出异常当它遇到一个DB错误而不是显示这样的消息的:CodeIgniter - 如何捕获数据库错误?
数据库出错错误编号:1054 未知列“富“在 'where子句' SELECT * FROM(
FooBar
)WHEREfoo
= '1'
注:我只希望这在一个控制器发生。在其他控制器中,我很高兴能够显示DB错误消息。
尝试这些CI功能
$this->db->_error_message(); (mysql_error equivalent)
$this->db->_error_number(); (mysql_errno equivalent)
也许这:
$db_debug = $this->db->db_debug; //save setting
$this->db->db_debug = FALSE; //disable debugging for queries
$result = $this->db->query($sql); //run query
//check for errors, etc
$this->db->db_debug = $db_debug; //restore setting
您必须关闭调试关闭在配置/ database.php中的数据库 - >
$db['default']['db_debug'] = FALSE;
这是更好为您的网站安全。
使用它
$this->db->_error_message();
这是一个寻找error.After完成您的网站。 使用它
$db['default']['db_debug'] = FALSE;
你就会改变你的config文件夹中的database.php中
我知道这个线程是旧的,但万一有其他人有这个问题,关闭错误信息 。这是我使用过的一个技巧,而不用触及CI db类。将您的调试保留在错误视图文件中,并引发异常。
所以在你分贝的配置,您有:
$db['default']['db_debug'] = true;
然后在你的数据库的错误视图文件,我的是在application/errors/error_db.php
替换所有的内容有以下:
<?php
$message = preg_replace('/(<\/?p>)+/', ' ', $message);
throw new Exception("Database error occured with message : {$message}");
?>
由于视图文件将被调用,错误将总是作为异常抛出,您可能会在以后为不同的环境添加不同的视图。
在笨3.0(CI3),所有你需要做的就是$this->db->error()
如果你需要得到已发生的最后一个错误,该错误()方法返回一个包含其代码和消息的数组
http://www.codeigniter.com/user_guide/database/queries.html#handling-errors
Thx!他们没有在他们的CI 2 - > 3迁移指南中提到这个! – user3319803
Wonderful ,你救了我的一天..我仍然在做CI 2 – Neri
将这个代码在一个名为MY_Exceptions文件。在应用程序/核心文件夹中的PHP:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* Class dealing with errors as exceptions
*/
class MY_Exceptions extends CI_Exceptions
{
/**
* Force exception throwing on erros
*/
public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
set_status_header($status_code);
$message = implode("/", (!is_array($message)) ? array($message) : $message);
throw new CiError($message);
}
}
/**
* Captured error from Code Igniter
*/
class CiError extends Exception
{
}
它会使所有的Code Igniter错误被视为Exception(CiError)。然后,打开所有的数据库调试:
$db['default']['db_debug'] = true;
我已经创建了一个简单的库为:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class exceptions {
public function checkForError() {
get_instance()->load->database();
$error = get_instance()->db->error();
if ($error['code'])
throw new MySQLException($error);
}
}
abstract class UserException extends Exception {
public abstract function getUserMessage();
}
class MySQLException extends UserException {
private $errorNumber;
private $errorMessage;
public function __construct(array $error) {
$this->errorNumber = "Error Code(" . $error['code'] . ")";
$this->errorMessage = $error['message'];
}
public function getUserMessage() {
return array(
"error" => array (
"code" => $this->errorNumber,
"message" => $this->errorMessage
)
);
}
}
这个例子查询:
function insertId($id){
$data = array(
'id' => $id,
);
$this->db->insert('test', $data);
$this->exceptions->checkForError();
return $this->db->insert_id();
}
而且我能赶上这样说在我的控制器中:
try {
$this->insertThings->insertId("1");
} catch (UserException $error){
//do whatever you want when there is an mysql error
}
而且当查询a重新创建,'$ this-> db-> last_query()'也是有用的。 – uzsolt
这些如何防止消息显示? – StackOverflowNewbie
你必须在config/database.php - > $ db ['default'] ['db_debug'] = FALSE; – decebal