Welcome to TechQura.com

Helping users, developers and technologists to write and find solutions
Techqura.com > Coding > PHP > Q. How to display MySQL error in PHP ?
Techqura.com

How to display MySQL error in PHP ?

Asked by : James Potter on 23/02/2021
Response by TechGuy



Display MySQL error in PHP

A hosting server might produce many different types & levels of earning and errors during PHP code execution. Displaying these errors is essential for developers in order to troubleshoot faulty code and application.

Inline PHP code for displaying errors

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Configure .ini file to Display All PHP Errors

The display_errors directive must be set to "on" in the PHP ini file. This will display all the PHP errors including syntax or parse errors that cannot be displayed by just calling the ini_set function in the PHP code. The PHP ini file can be found in the displayed output of phpinfo() function and is labelled loaded configuration file.

Display PHP Errors via .htaccess

To make changes on .htaccess file, you must have access to the directory files. .htaccess file located in the root or public directory of the server. Add below lines :
php_flag display_startup_errors on
php_flag display_errors on
Note: wrong configuration in .htaccess file might cause issue with your website. Please be careful.

Database error PHP code

The mysqli_error() function is identical to the corresponding mysqli_errno() function in every way, except instead of returning an integer error code the mysqli_error() function will return a string.
mysqli_query($this->db_link, $query) or die(mysqli_error($this->db_link));
# mysqli_query($link,$query) returns 0 if there's an error.
# mysqli_error($link) returns a string with the last error message


You can also use below code to print the error code :

echo mysqli_errno($this->db_link);

For more info check https://www.php.net/manual/en/mysqli.error.php