4

我在 CodeIgniter 中使用活动记录。
我可以成功地将数据插入数据库。
但是失败时如何得到insert sql的结果呢?
现在它返回一个关于 sql 错误的 html 语句。
我不想要这个 html 内容。

编辑

$data = array(
   'title' => 'My title' ,
   'name' => 'My Name' ,
   'date' => 'My date'
);

$this->db->insert('mytable', $data); 

简单的代码。但是当“名称”列具有唯一属性时。我正在向它插入一个重复的值。它返回 sql 错误的 html 内容。

我只希望它返回错误代码而不输出 html 内容。

4

2 回答 2

15

To not get the DB error message make sure that you have $db['default']['db_debug'] = FALSE in the file /application/config/database.php.

Then after you've preformed your (attempted) insert you can run:

$num_inserts = $this->db->affected_rows();

If the result is 0, your insert failed and you can present an error message of your own choosing.

于 2012-06-19T07:16:37.940 回答
2

使用这些功能

$errNo   = $this->db->_error_number();
$errMess = $this->db->_error_message();

他们会帮助

于 2012-06-19T09:35:14.360 回答