1

I would like to get return info from $CI->db->insert(...), both on success and failure.

I have tried to simply catch the return and echo it and print_r it, but neither gives me anything useful; And I have tried $CI->db->display_errors() (though I did not actually find it listed at CI docs).

$return = $this->db->insert( "some_table" , $table_data ) ;

print_r( $return  ) ; // 
// OR
print  ( $this->db->display_errors() ) ;

Does $CI->db->insert(...) offer a way to check success/failure?


UPDATES:

in CI3 ...

$this->db->_error_message() generates an error: Fatal error: Call to undefined method CI_DB_mysqli_driver::_error_message() (with or without the leading underscore).

$this->db->display_errors() generates an error: Fatal error: Call to undefined method CI_DB_mysqli_driver::display_errors()

$this->db->insert_id() generates a 0 (regardless of whether the insert succeeds or fails)

dsdsdsdsd
  • 2,880
  • 6
  • 41
  • 56

2 Answers2

4

$this->db->_error_message() is deprecated so instead of that try use following function.

$error = $this->db->error(); 
Fabulous
  • 2,393
  • 2
  • 20
  • 27
0

There are two methods that give me meaningful info, though I haven't seen in the CI3 documentation that the info is definitive, thus I consider this a weak-ish solution:


A ...

$return = $this->db->insert( "some_table" , $lvl_data ) returns...

// on SUCCESS: 1 ......... which indicates the boolean TRUE, not the number 1
// on FAILURE: nothing ... which indicates the boolean FALSe

However, I do not know from the docs if this is definitively always ensured.

PLEASE NOTE: this will generate an uncatch-able formatted CI error message on failure, unless you use affect $this->db->db_debug ... see working example below for usage.


B ...

$this->db->error() returns...

// on SUCCESS: Array ( [code] => 0    [message] => ) 
// on FAILURE: Array ( [code] => 1054 [message] => Unknown column 'some_bad_column_name' in 'field list' ) 

However, I do not know for certain (based on CI3 doc) that [code]=>0 necessarily equals success.


A+B, with $this->db->db_debug ...

Thus I believe that combining these two infos is loosely indicative:

$db_debug           = $this->db->db_debug ; //save setting
$this->db->db_debug = FALSE               ; //disable debugging for queries

$return = $this->db->insert( "some_table" , $lvl_data ) ;
$error  = $this->db->error ()                           ;
if ( $error['code'] === 0  &&  $return === true )
  { // almost surely SUCCESS
  }

$this->db->db_debug = $db_debug           ; //set it back
dsdsdsdsd
  • 2,880
  • 6
  • 41
  • 56