CodeIgniter中的数据库错误处理问题 [英] Database Error Handling problem in CodeIgniter
本文介绍了CodeIgniter中的数据库错误处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用CodeIgniter作为我的Web应用程序框架.我使用了一个简单的Try/Catch,并发送了一个样本值对其进行测试,但失败了!
I use CodeIgniter as my web application framework. I used a simple Try/Catch and I sent a sample value to test it, and it failed!
我知道我可以使用$this->db->escape()
函数解决我的数据问题,但我只想知道:为什么TRY/CATCH无法捕获此错误!
I know I can use $this->db->escape()
function to solve my data problem but I just want to know: Why TRY/CATCH can not catch this error!
控制代码:
$this->load->model('user_model');
$result = $this->user_model->test_user("tes'ti");
型号代码:
function test_user($username){
try {
$query_str = "SELECT * FROM tbl_user WHERE username = '".$username."'";
$result = $this->db->query($query_str);
return $result;
} catch (Exception $e) {
return;
}
}
输出:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ti'' at line 1
SELECT * FROM tbl_user WHERE username = 'tes'ti'
如果我做错了,请告诉我,我在哪里弄错了!
Let me know, where I made a mistake, if I did!
推荐答案
如果出现某些mysql错误,则需要引发异常:
You need to throw an exception if there was some mysql error:
try {
$query_str = "SELECT * FROM tbl_user WHERE username = '".$username."'";
$result = $this->db->query($query_str);
if (!$result)
{
throw new Exception('error in query');
return false;
}
return $result;
} catch (Exception $e) {
return;
}
这篇关于CodeIgniter中的数据库错误处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文