Where to find all possible errors in MySQLi

2k views Asked by At

I want to create a custom error system in MySQLi, for this I will need a list with all error codes and a description on why they are being caused.

I have searched all over the web but can't find anything on php.net


For example if I create a new object where the host is invalid:

$mysqli = new mysqli('invalid_host', 'root', '', 'db');

then the error code will be 2002 which means it could not find the adress.

The original error looks like:

php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known

But I would want it to display:

Connection error! There was an problem connecting to the database, this is caused by your invalid hostname: "invalid_host" which seems to not be connectable or pingable! (2002)


In order to do that I will need to know all the errors that can occure in MySQLi, I haven't found a list yet so I am asking here if you know about a list


If you wonder how I'm going to do it I am planning to have an array which will contain of:

$error_list = [
    2002 => [
        'category' => 'connection_errors',
        'error_title' => 'Connection error!',
        'error_content' => 'There was an problem connecting to the database, this is caused by your invalid hostname: "invalid_host" which seems to not be connectable or pingable!',
        'error_code' => '2002',
        'error_description' => 'This error is occurred by invalid hostname. Why is this error occurring? It is occurring because you have entered invalid hostname, such as: "msql. example .com"'
    ]

    // And so on
];

I'd be really happy if anyone know where to find such a list, this project will be shared in the future, and when it's finished I'll add an comment with an link.


The project can also make easier and safer (prepared) queries to an MySQL server, with MySQLi.

1

There are 1 answers

0
Your Common Sense On

But I would want it to display:

You don't want it. Simply because you're not the sole user of your site.

For a site user, it's still a gibberish. Not to mention that a honest user will be rather bewildered being accused for "providing the invalid hostname"!
If you take any mature site, it never shows anything like that. Just a generalized error page says "There is an error, we're already on it".

For a programmer, all this stuff beside the actual error message is just useless rubbish, that litters the actual and useful information. Instead of all that useless stuff, better add a stack trace, the actual SQL query, some critical variables. Again, learn from big boys: good framework will show you not only error message but even the code snippet where the error occurs.

With all the due respect, I understand your idea to improve mysqli. But you have to check your premises first. It is very important to run a live site for some time to understand the real purpose of error reporting.

Imagine I'll be in charge for your site after you finish it, and it emits this kind of error. Even if I'd have no idea what does the error message mean, I can google it up. The original error message will give me a 1000 results, while your custom one will give me nothing. Please, do not re-tell the error message with your own words - you may lose the important part. Neither should you litter the error message with some useless descriptions. Just leave it alone and make sure that a programmer will read it.