user2481984
user2481984

Reputation:

Hide Codeigniter error and echo a customized error

When I try to connect to a database, I get this error:

A PHP Error was encountered

Severity: Warning

Message: mysqli::real_connect(): (HY000/1044): Access denied for user ''@'localhost' to database 'dm_receiver'

Filename: mysqli/mysqli_driver.php

Line Number: 133

Backtrace:

File: C:\xampp\htdocs\DataMigrator\ci_my_app\models\Connection_model.php Line: 111 Function: database

File: C:\xampp\htdocs\DataMigrator\ci_my_app\models\ReadDatabase_model.php Line: 7 Function: establish

File: C:\xampp\htdocs\DataMigrator\ci_my_app\controllers\Home.php Line: 7 Function: model

File: C:\xampp\htdocs\DataMigrator\index.php Line: 292 Function: require_once

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Filename: C:/xampp/htdocs/DataMigrator/ci_my_app/models/ReadDatabase_model.php

Line Number: 7

I have specified the wrong username on purpose to test what happens when I can't get a database connection. Well, that's what I get. I would like to hide this error and echo my own message if this happens. How do I do that?

Edit: Let my ask my question another way. When I use $this->db->get('mytable'); I might get the same error. This could be because either the hostname, database name, username or password is wrong. So how could I find out which one of them is not correct and echo like for example watch out, your database name is wrong?

Upvotes: 1

Views: 3030

Answers (1)

praniclift
praniclift

Reputation: 126

You can turn off error messages in production by editing your index.php file:

define('ENVIRONMENT', 'production');

Then use your code to check the conditions of what comes out of your database, or edit application/errors/error_db.php to define a custom error page.

Upvotes: 3

Related Questions