views:

37

answers:

3

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. ;)

A: 

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;
}
Sarfraz
Thanks Sarfraz, I did your recommendations but it failed again! do you have more ideas?!
Monica
@Monica: What error/output came about when you did this?
Sarfraz
I've mentioned the error in Output part of my question... ;)
Monica
@Monica: Then i am pretty much sure it is handled/done so by the `query` function `$this->db->query`. If you try the same code with simple `mysql_query` command for example in a test page, it should catch it.
Sarfraz
@Sarfraz: WOW! So it's because I use CodeIgniter?! Are you sure?! So I must google about CodeIgniter Database Error Handling problem! Is it true?!
Monica
@Monica: Look possibly the `query` function either returns the result or outputs the result by throwing an exception or using `mysql_error()` function, you might want to check that to confirm it just in case.
Sarfraz
@Sarfraz: You help me to find the source of problem and the way to solve it! I found that the CI Database class contained its error handling part and it doesn't throw any exception at all! I turned it off (if anybody want to know, do this: in config/database.php -> $db['default']['db_debug'] = FALSE;) .... My TRY/CATCH works properly now. Thank you really much ;)
Monica
@Monica: So my clue was right, the code was fine :)
Sarfraz
@sarfraz: Yes, that's right ;) tashakor
Monica
A: 
Jean
Thanks jean, but it's not my problem... I just want to fix my try/catch problem... actually you can do a search with this word "tes'ti", you must use addslashes() in php or escape() in CI... ;)
Monica
@monica The error triggers in your query statement. I have never used try and catch, actually never found the need to be honest.
Jean
@Jean: I read your comment again, and now I get your mean! :D thanks
Monica
@monica I never found the need to put in try/catch
Jean
@Jean I don't want to show SQL Script Error to end users, indeed CI shows any database exception completely in its format! I want to show a simple error, not a detail error about my database! So I need to manage it myself. :P
Monica
@monica, the script will not show an error, unless the variables passed is incorrect, check variables before passing
Jean
thanks for your advice. ;)
Monica
A: 

@Monica, not sure if this helps, but you should know that the CI database functions never throw any errors. They just return true or false. Therefore @Sarfraz is right, you must either check for true/false yourself and/or throw Exceptions yourself.

Also, your exception handling code does nothing. This means it will continue running any scripts coming after it, including scripts that work with the recordset that just failed.

Ferdy
@Fredy, do you mean even when I set "$db['default']['db_debug'] = FALSE" it does not throw any exception?! ... And about that code, you're right but that was just a sample to show my problem! :P
Monica
@Monica. Yes, that is correct. No CI db code will EVER throw an error. It will only return true or false, after which you must throw and/or catch an error.
Ferdy