Hi,
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!
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!
Controler code:
$this->load->model('user_model');
$result = $this->user_model->test_user("tes'ti");
Model code:
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;
}
}
Output:
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!
Thank you for your time and helping others. ;)