U
U
uaf0x2017-09-30 02:02:21
CodeIgniter
uaf0x, 2017-09-30 02:02:21

How to handle database connection errors in Codeigniter?

During installation, the user enters his data for connecting to the database, you need to process them and in case of some error try to suggest what the problem is (name \ host \ password), I try to connect to the database with incorrect data, an error just falls out, how can I process it so that I could mistakenly write hints to the user. Googled, googled for a long time, googled in different languages, googled nothing (

public function dbtest(){
    $host=htmlspecialchars(trim($this->input->post('dbhost')));
    $user=htmlspecialchars(trim($this->input->post('dbuser')));
    $password=htmlspecialchars(trim($this->input->post('dbpassword')));
    $database=htmlspecialchars(trim($this->input->post('dbname')));

    $databseConfig = array(
        'hostname'=>$host,
        'username'=>$user,
        'password'=>$password,
        'dbdriver' => 'mysqli',
    );

    $this->load->database($databseConfig);
  }

Forgive me for being so chaotic, my eyes are already closing (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Wolf, 2017-09-30
@Wolfnsex

How to handle database connection errors in Codeigniter?
Probably the solution that I will offer is not the best in terms of architecture ... but CodeIgniter itself is not a masterpiece of architectural solutions in its field.
We argue logically.
1. CodeIgniter is written in pure PHP and CodeIgniter cannot do anything beyond what PHP itself can do.
2. We have some difficulties with handling connection errors to the database at the CodeIgniter level (it doesn’t matter which ones exactly), but the fact is that the CI option doesn’t suit you somehow
3. Nothing prevents us from connecting to the database, bypassing the standard CI mechanism (I mean, a test connection, and not "generally work bypassing the standard mechanisms")
Further, the solution suggests itself:
1. Connect to the database "directly", for example, using mysqli_connect (with the data entered by the user)
2.A Check the connection and if "not connected", check the error, for example using mysqli_error
2.B but we get not an error message, but its code, the mysqli_errno
function The function will return you an error (text) or its number (code), which you can further process as you like. At the same time, a "test connection" can be done both inside the controller, there and generally in some separate file that exists separately from CI and serves, for example, for its installation. In other words, the further depends on your imagination and ultimate needs.
PS Correspondingly, if initially, MySQL does not return a specific error, for example, "the login is incorrect" or "the login is correct, but the password is not" (which in principle would be logical, for security reasons) - and you need just such a meticulous degree error details - it will not be possible to get it in some reasonable way, but errors like "MySQL server at address N - no" or "Connection credentials are not correct" - it will not be difficult to process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question