Question

How do i get a detailed error description during a php-mysql script run?

I have the following statements where the script fails and displays the custom error message - the contents of the "or die".

I want to get the actual error from MySQL (instead of the custom error i have mentioned), which would give better idea about the scenario - whether its a database issue or server connectivity issue etc..

this is the code i have where i need to enhance the error reporting

$query = "SELECT * FROM table_name";
$result = mysqli_query($db_conn, $query) 
  or die('Connected to database, but querying failed');

thanks!

Was it helpful?

Solution

Have a look at the manual page for mysqli_error. In the section "Procedural style" you'll find a complete example that shows how to set up the database connection and querying the database, both steps with error handling.

If you want detailed error information from your scripts you can put the lines

error_reporting( E_ALL );
ini_set('log_errors', true);
ini_set('error_log', '/tmp/php-errors.log');

at the start of your code. That way all errors coming from PHP will be written into the log file. Make sure that the path to the log file exists (/tmp is only an example) and is writable by the web server, otherwise the errors will be silently discarded.

As a side note: While the "or die()" pattern is good for small examples, you should not use it in production code.

OTHER TIPS

Check out mysql_error function.

I prefer using PDO, and having PDO throw an exception.

you could insert the query into the query page on phpmyadmin that is if you are using it. But this wont tell you if the errors are with the variables

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top