17

I have an Codeigniter app (using version 2.1.0) that is writing a transaction to a mysql database. I'm fairly sure that I've got a foreign key constraint error occurring, but I can find no way to make CI tell me the specific error. mysql_error() comes back empty.

Can anyone tell me how to get Codeigniter to tell me the mysql error message?

Dharman
  • 30,962
  • 25
  • 85
  • 135
pbarney
  • 2,529
  • 4
  • 35
  • 49

2 Answers2

40

Yes, this is the mysql_error() wrapper.

$this->db->_error_message();

And the mysql_errno wrapper is:

$this->db->_error_number();
Bella
  • 3,209
  • 23
  • 26
  • In the current version of CI, those are private functions. Any suggestions on the best way to get access to them with modifying the core files? – pbarney Mar 27 '15 at 19:57
  • I don't get this, i mean how to use it. I Tried but get nothing in display. @DRL – always-a-learner Jan 18 '17 at 13:00
  • i've got this -> Call to undefined method CI_DB_mysqli_driver::_error_number(). i'm using php 5.6 – Mang Jojot Jan 16 '18 at 05:59
  • This answer is deprecated. .check this https://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors – AgentP Jul 10 '22 at 13:51
1

You may be able to use call_function in the db class to access mysql_error:

http://ellislab.com/codeigniter/user-guide/database/call_function.html

Of course, you could also just turn on the debug flag in the DB config, to tell CI to display db errors:

http://ellislab.com/codeigniter/user-guide/database/configuration.html

db_debug - TRUE/FALSE (boolean) - Whether database errors should be displayed.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • When I attempt to use call_function('mysql_error'), I get the following: "This feature is not available for the database you are using." It is an innodb table under mysql. I do have $db['default']['db_debug'] = TRUE in the database.php config file. The most I get is my exception and a stack trace. This is so frustrating. Right now, I'm feeling like going with CI was a big mistake. – pbarney Jul 13 '10 at 06:16
  • @pbarney dont give up on it yet, check my answer – Bella Jul 13 '10 at 06:47
  • 1
    Also note, as mentioned on the `call_function` page, you're supposed to pass the name of the function **without** the `mysql_` prefix. – Amber Jul 13 '10 at 06:49