4

I have been trying for three days now to enable error reporting in PHP. I have gotten by for a while using the ini_set('display errors' 1); function until I tried to connect to a DB; it didn't work. Now, I have enabled error_reporting, display_startup_errors, log_errors without any effect on error reporting. I have changed all five config files (the development ini, production ini, the php.ini file(s) located in php/7.0/cli, php/7.0/fpm, and even the one in apache2 (even though I am running nginx)

I am beginning to doubt my own abilities, any assistance is greatly appreciated.

EDIT: I have used the ini_set function described above in my files, and it worked up until I tried to connect to a DB. I have confirmed that I've enabled error reporting for the php.ini file described in the phpinfo() function directory path. No effect whatsoever.

11
  • In most cases you can just add: ini_set('display_errors', 1); error_reporting(E_ALL); at the beginning of your script and get errors displayed like that. If you want to do that via config, make a phpinfo() script, see where which config file is loaded and alter that file until phpinfo() shows error reporting and display errors that you want to have. Commented Feb 10, 2016 at 22:48
  • 1
    Possible duplicate of How do I get PHP Errors to display? Commented Feb 10, 2016 at 22:49
  • You lead me back to where I started :o Thank you though, I will continue searching for an answer! Commented Feb 10, 2016 at 23:02
  • What command you use to db connection? Commented Feb 10, 2016 at 23:10
  • Both error_reporting and display_errors are not set properly if you're not seeing errors. Do a test: echo HELLO; if(array_key_exists($a, $b)) echo "Hello"; Do you see warnings? If not, your config is absolutely not setup properly. If so, it's your DB adapter. You'll have to do error catching. Commented Feb 10, 2016 at 23:13

5 Answers 5

8

Because no one particularily gave away the answer, I will just have to post it myself.

I found the error.log file (which indeed is logging all errors on my Nginx server) in this directory: /var/log/nginx/error.log

Hopefully this may help others using Nginx as well, but I still do not understand why the **** the errors aren't showing up in the browser. I think it is Nginx's nature to make everything quite complicated.

Perhaps I should develop using Apache and then port it into Nginx when I have more experience -- just some thoughts for others who are getting into this as well.

I just wanted to give an update on this: Since upgrading from PHP 7.0.2 <= 7.0.3, I am now able to see the errors that should have been displayed.

EDIT: Don't delete the contents of that log file, it will screw the whole error reporting. I'm back to nothing now. –

Sign up to request clarification or add additional context in comments.

1 Comment

Just a quick note: This has absolutely nothing to do with PHP error reporting. This is a web-server-level configuration issue. This answer is in no way related to error reporting and PHP. It is strictly related to nginx's configuration, and how it wraps PHP's error reporting (and any other choice of scripting language). It's great that the OP figured his issue out, but it really is worded, tagged, and answered 100% incorrectly. I wish I had seen this sooner, but alas, anyone seeing this now should at least be leery of very misleading question/answer relationship.
4

Error Reporting Itself

ini_set('display_errors', 1); or display_errors

Simply allows PHP to output errors - useful for debugging, highly recommended to disable for production environments. It often contains information you'd never want users to see.

error_reporting(E_ALL); or error_reporting

Simply sets exactly which errors are shown.

Setting one or the other will not guarantee that errors will be displayed. You must set both to actually see errors on your screen.

As for setting this up permanently inside your PHP config, the default for error_reporting is E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED. That said, this variable should not need changed. See here:

http://php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

As for displaying errors, see here:

http://php.net/manual/en/errorfunc.configuration.php#ini.display-errors

Set the config value of "display_errors" to either stderr or stdout, depending on your need.

Just change these variables inside of your php.ini file and you'll be golden. Make absolutely sure both display_errors and error_reporting is set to a satisfactory value. Just setting error_reporting will not guarantee that you see the errors you're looking for!


Error Reporting Works Everywhere Except When Connecting To My DB!

If you see errors everywhere you need to except in the Database Connection, you just need to do some error catching. If it's PDO, do something like this:

try {
    $this->DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $this->DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $STH = $this->DBH->prepare("INSERT INTO `" . $this->table . "` ($fs) value ($ins) $up");
            
    $STH->execute($data);
            
    $id = $this->DBH->lastInsertId();
            
    $this->closeDb();
            
    return $id;
} catch(PDOException $e) {
    echo $e->getMessage();
}

Just a snippet from my framework. Of course you'll have to change it to your liking, but you should be able to get the general idea there. They key is this part here:

try {
    //DB Stuff
} catch(PDOException $e) {
    echo $e->getMessage();
}

I Still Don't See The Error

If you've done both of what I've listed here and still have trouble, your problem has nothing to do with enabling error reporting. The code provided will show you the error with a Database Connection itself, and inside of PHP code. You must have a completely different issue if this has not shown you an error you're chasing.

You'll likely need to be a bit more descriptive on exactly what you're chasing, and what you're expecting to see.

9 Comments

The note about PDO is ambiguous. The OP write that error reporting fails on db connection. You interpret (maybe properly) the db connection as PDO, but your example is not about PDO connection, it's about PDO commands following connection. Actually, error reporting has effect on PDO establishing connection.
I followed everything in your post and it is still not working. I am wondering if this has anything to do with me running nginx?
@fusion3k If a connection is not made, simple error_reporting and display_errors will display an error when trying to call a PDO method like prepare() or execute(). Your comment is borderline irrelevant as my provided code will show errors no matter what the case is. A method error on prepare() or execute() (etc.) means you never made a connection - it's very simple. Also, I provided an example of try and catch - I never stated it had to be after the connection. What part of //DB Stuff indicates that it can't be used on connection?
@Frank It very well may be nginx. What exactly doesn't work? Do you see any errors at all? Like if you type gibberish like so: echo $fake->nothing(HELLO); will it give you any sort of errors? This should produce an error similar to Call to undefined method (forget the exact verbiage). It may be worth your time to throw the code in question on an Apache server to see if it gives you more info as to what the error itself is. It's not ideal, but that'll at least confirm if it's nginx.
@NateI Thank you for the quick replies Nate, I greatly appreciate it. If I run this code: echo $undefinedvar; it will not return the error #8: Undefined Variable like it's supposed to, even though I have clearly enabled error reporting in the php.ini file described in the phpinfo() page! I can already assume it will work on Apache, although I will try anyways.
|
2

Try:

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

1 Comment

That doesn't work all the time. I need a permanent solution that involves modifying my php config. Thank you though.
0

perhaps, it will help you. change values of parameteres in the file /etc/php/7.0/fpm/pool.d/www.conf (for example value display_errors by default is disabled)

Comments

0

I could find the errors in /var/log/php7.4-fpm.log

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.