2011-10-20 100 views
47

有没有一种方法,使CI抛出异常当它遇到一个DB错误而不是显示这样的消息的:CodeIgniter - 如何捕获数据库错误?

数据库出错错误编号:1054 未知列“富“在 'where子句' SELECT * FROM(FooBar)WHERE foo = '1'

注:我只希望这在一个控制器发生。在其他控制器中,我很高兴能够显示DB错误消息

回答

47

尝试这些CI功能

$this->db->_error_message(); (mysql_error equivalent) 
$this->db->_error_number(); (mysql_errno equivalent) 
+3

而且当查询a重新创建,'$ this-> db-> last_query()'也是有用的。 – uzsolt

+6

这些如何防止消息显示? – StackOverflowNewbie

+7

你必须在config/database.php - > $ db ['default'] ['db_debug'] = FALSE; – decebal

23

也许这:

$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 
13

您必须关闭调试关闭在配置/ database.php中的数据库 - >

$db['default']['db_debug'] = FALSE; 

这是更好为您的网站安全。

2

使用它

$this->db->_error_message(); 

这是一个寻找error.After完成您的网站。 使用它

$db['default']['db_debug'] = FALSE; 

你就会改变你的config文件夹中的database.php中

9

我知道这个线程是旧的,但万一有其他人有这个问题,关闭错误信息 。这是我使用过的一个技巧,而不用触及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}"); 

?> 

由于视图文件将被调用,错误将总是作为异常抛出,您可能会在以后为不同的环境添加不同的视图。

1

将这个代码在一个名为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; 
4

我已经创建了一个简单的库为:

<?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 

}