Welcome Guest, Not a member yet? Register   Sign In
How to retrieve database error from CodeIgniter
#11

[eluser]adwin[/eluser]
use this
Code:
$this->db->_error_message();

will return empty if no error.
#12

[eluser]VSz[/eluser]
Thanks.
One more question, how can I stop the CodeIgniter error page showing up when my query fails? Is there another way besides turning off error_reporting() in my index.php, and adding each and every error reporting (without the database errors) back in manually?
#13

[eluser]Michael Wales[/eluser]
In ./config/database.php:
Code:
$db['default']['db_debug'] = TRUE;

Set it to false.
#14

[eluser]VSz[/eluser]
Wow, thanks.
This reminds me of the old heydays of Pascal, where exception handling consisted in turning the IO checking off and on, directly from the compiler directives Smile
#15

[eluser]hjeffg[/eluser]
I also needed this functionality since I will be doing tons of inserts with only occasional duplicate entries that I want to handle separately. I don't think it's a great idea to have to do a 'get' to see if the record already exists each time.

SO.... I did the following:

I modified DB_active_rec.php by adding the function insert_quiet()

function insert_quiet($table = '', $set = NULL)
{
$oldv =$this->db_debug;
$this->db_debug = false;
$this->insert($table, $set);
$e = $this->_error_message();
$aff = $this->affected_rows();
$this->db_debug = $oldv;
if($aff < 1) {
return($e);
} else {
return(true);
}
}

Now, I know I *should* have done this by extending the class, but I wasn't sure how all the default database stuff that goes on would work with my extended class. Perhaps when I learn more, I'll do that. Or, if someone else knows how to, please advise. Meanwhile, this is working for me.
#16

[eluser]DarthVinsus[/eluser]
[quote author="adwin" date="1211548804"]use this
Code:
$this->db->_error_message();

will return empty if no error.[/quote]


the error number $this->db->_error_number(), where I can look? I will know if there is a list of these error and whre I can look for it?
#17

[eluser]smidoid[/eluser]
Why over-complicate it?

Code:
$this->db->simple_query

Does the same thing (faster) and doesn't return ANY error: so use with caution!

This is a public method on line 440 of DB_driver (in 1.7.0) anyway. ;-)

There's a very good argument to allow PHP to ignore duplicate inserts in some rare cases - and it's quicker to ignore the error in the same way as you might use "@" in PHP if you know it might happen.
#18

[eluser]iMefisto[/eluser]
I'm doing this:

in my model, delete example:

Code:
function del($id) {
  $this->db->where('id', $id);

  $this->db->delete('mytable');    
        
  return $this->db->_error_number()==0;

}

if return is false, then I can handle the error with $this->db->_error_number
#19

[eluser]Unknown[/eluser]
I realize this is an old post, just figured I'd share what I've found on the topic.

If you're using the OCI driver, you can capture the most recent error with this:

Code:
oci_error( $this->db->stmd_id )

Which will return (in the event of a unique constraint error)

Code:
array(4) {
  ["code"]=>
  int(1)
  ["message"]=>
  string(74) "ORA-00001: unique constraint (DB.TABLE_UK1) violated"
  ["offset"]=>
  int(0)
  ["sqltext"]=>
  string(90) "INSERT INTO FAKETABLE (report_id, role_id, var_list) VALUES ('365','1','1090')"
}

I realize this could potentially be wrapped in the DB class somewhere but I'll leave that up to the reader to decide.




Theme © iAndrew 2016 - Forum software by © MyBB