2

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.

Gnusam
  • 38
  • 6
Frank
  • 119
  • 1
  • 1
  • 10
  • 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. – Ilija Feb 10 '16 at 22:48
  • 1
    Possible duplicate of [How do I get PHP Errors to display?](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – rjdown Feb 10 '16 at 22:49
  • You lead me back to where I started :o Thank you though, I will continue searching for an answer! – Frank Feb 10 '16 at 23:02
  • What command you use to db connection? – fusion3k Feb 10 '16 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. – Nate I Feb 10 '16 at 23:13
  • @fusion3k $conn=mysqli_connect($host,$dbuser,$password,$dbname); if(mysqli_connect_errno()) { die("Connection Failed! " . mysqli_connect_error()); } else { echo "Connected to Database ($dbname)"; } – Frank Feb 10 '16 at 23:30
  • @Natel those functions have worked in all cases except when connecting to my DB. Also, after running those commands it did not return any errors. What might be the problem with my config? Where should I even begin? – Frank Feb 10 '16 at 23:36
  • @Frank on `mysqli_connect` `ini_set('display_errors', 1)` with `error_reporting( E_ALL )` should produce a _Warning_ message; `error_reporting` w/out `ini_set` could not print out anything. – fusion3k Feb 10 '16 at 23:41
  • what is the value of log_errors in php.ini ? These errors may be logging to a file instead of being displayed (good idea). Do the errors show up in the error_log ? – Jeff Feb 11 '16 at 00:30
  • I have succesfully found the error log, it was being stored in /var/log/nginx/error.log This allows me to see the errors being produced, in perhaps a more secure, but less efficent way. Although this has not completely solved my question, I should be able to proceed with development. Thank you all for your help and making me feel welcome with my first post on stack overflow! – Frank Feb 11 '16 at 01:13
  • @Frank You're using php7-fpm, correct? – Nate I Feb 17 '16 at 14:22

4 Answers4

6

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. –

peterh
  • 9,698
  • 15
  • 68
  • 87
Frank
  • 119
  • 1
  • 1
  • 10
  • 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. – Nate I Sep 25 '18 at 16:21
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.

Community
  • 1
  • 1
Nate I
  • 914
  • 4
  • 10
  • 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. – fusion3k Feb 10 '16 at 23:27
  • 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? – Frank Feb 10 '16 at 23:28
  • @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? – Nate I Feb 10 '16 at 23:30
  • @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. – Nate I Feb 10 '16 at 23:34
  • @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. – Frank Feb 10 '16 at 23:47
  • @fusion3k Heh, when I highlight a `try` and `catch` it absolutely **is** something that can be used in connection. Simply because you're expecting a copy and paste solution (which is impossible anyway - unless you'd like to post your DB credentials for the world to see) and can't imagine how you'd utilize the code, doesn't mean the solution isn't to do with connection error catching. Especially when I explicitly state it will need to be changed and the key point is the `try` and `catch` functionality itself. Just learn to read and you'll be relevant :) – Nate I Feb 16 '16 at 20:34
  • @NateI Your comment has nothing to do whit my comment! – fusion3k Feb 16 '16 at 21:21
  • @fusion3k Why are you even posting? Judging by all the answers in your history, you don't even have a solid grasp on PHP - you have not answered a single question that's even remotely difficult. People like you who'd rather jump in and nit-pick in hopes of receiving quick, free votes rather than trying to contribute to the community and **actively help people who are new to programming** are the bane of Stack Overflow. Either help people out, or GTFO. Your opinions are unneeded, unfounded, and utterly unhelpful to *any* person stumbling upon answers. – Nate I Feb 16 '16 at 23:30
  • @NateI Yes, you are right. Thank you for appreciation. – fusion3k Feb 16 '16 at 23:37
2

Try:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Mixcoatl
  • 150
  • 1
  • 6
  • 1
    That doesn't work all the time. I need a permanent solution that involves modifying my php config. Thank you though. – Frank Feb 10 '16 at 23:01
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)