How to Get Useful Error Messages in PHP

How can I get useful error messages in PHP?

For syntax errors, you need to enable error display in the php.ini. By default these are turned off because you don't want a "customer" seeing the error messages. Check this page in the PHP documentation for information on the 2 directives: error_reporting and display_errors. display_errors is probably the one you want to change. If you can't modify the php.ini, you can also add the following lines to an .htaccess file:

php_flag  display_errors        on
php_value error_reporting 2039

You may want to consider using the value of E_ALL (as mentioned by Gumbo) for your version of PHP for error_reporting to get all of the errors. more info

3 other items: (1) You can check the error log file as it will have all of the errors (unless logging has been disabled). (2) Adding the following 2 lines will help you debug errors that are not syntax errors:

error_reporting(-1);
ini_set('display_errors', 'On');

(3) Another option is to use an editor that checks for errors when you type, such as PhpEd. PhpEd also comes with a debugger which can provide more detailed information. (The PhpEd debugger is very similar to xdebug and integrates directly into the editor so you use 1 program to do everything.)

Cartman's link is also very good: http://www.ibm.com/developerworks/library/os-debug/

How do I get PHP errors to display?

This always works for me:

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

However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:

display_errors = on

(if you don't have access to php.ini, then putting this line in .htaccess might work too):

php_flag display_errors 1

How to get useful error messages in mysql query in php?

In the else-block you can see, how you can catch up MySQL-errors and display them in an alert.

if(isset($_POST['btnSubmit'])) {
$name = mysqli_real_escape_string($conn,$_POST["name"]);
$sql = "INSERT INTO isodetail(title) VALUES ('{$name}')";
$run = mysqli_query($conn, $sql);

if($run) {
echo "<script>alert('Certi Added Successfully');window.open('isocerti.php','_self');</script>";
} else {
$error = addslashes(mysqli_error($conn));
echo "<script>alert('An Error occur: {$error}');</script>";
}
}

References:
MySQLi-Error: http://php.net/manual/en/mysqli.error.php

Escape special characters: http://php.net/manual/en/function.addslashes.php (it does the job for this example)

User-Friendly PHP Error Message

Implement an error and exception handler

You need to write a custom error handler like this. As you can see at the bottom, I am introducing a FATAL error. Here PHP does not spit any ugly error messages as you have quoted. It would just print Some Error Occured. Please Try Later.

<?php

set_error_handler( "log_error" );
set_exception_handler( "log_exception" );
function log_error( $num, $str, $file, $line, $context = null )
{

log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
}

function log_exception( Exception $e )
{

echo "Some Error Occured. Please Try Later.";
exit();
}

require_once("texsss.php");// I am doing a FATAL Error here

Showing all errors and warnings

Display errors could be turned off in the php.ini or your Apache configuration file.

You can turn it on in the script:

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

You should see the same messages in the PHP error log.



Related Topics



Leave a reply



Submit