CodeIgniter - continue on SQL error?

后端 未结 2 564
一向
一向 2020-12-30 09:53

Basically I have a table with a couple of columns marked Unique. I have a script that dumps a bunch of values into the table with a command like this:

$this-         


        
相关标签:
2条回答
  • 2020-12-30 10:31

    Yeah, took me a while too and annoyed the hell out of me:

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

    ... in config/database.php - disables the error page.

    After a query ran, use this to check for an error:

    if (!empty($this->db->_error_message())) {
        echo "FAIL";
    }
    
    0 讨论(0)
  • 2020-12-30 10:54

    I know you already have a solution, but thought this might be useful for others viewing this question as well.

    Let the database do the work for you:

    $this->db->query("INSERT IGNORE INTO `table` (`col1`, `col2`, `col3`) VALUES (`val1`, `val2`, `val3`)");
    

    When you use INSERT IGNORE, things like duplicate key errors become warnings instead of errors, which let your queries run without interrupting the flow of your script.

    You could then do a

    SHOW WARNINGS;
    

    after all the queries have run to see what warnings occurred.

    http://dev.mysql.com/doc/refman/5.1/en/insert.html

    0 讨论(0)
提交回复
热议问题