Warning: mysqli_connect in PHP. Is this a serious note? - php

Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it. in E:\xampp\htdocs\ss\docs\regional.php on line 15
The connection to the database could not be established.
Is this a serious error or can easily be ignored? The error message 'The connection to the database could not be established.' is expected to pop but not the warning text. How do I remove it in that case. Below is the code I've used.
$db = mysqli_connect('localhost', 'root', '', 'regional_data');
if(mysqli_connect_errno()) {die('The connection to the database could not be established.');}
EDIT: I have stopped SQL in the development area to test this and the message occurs at that time.

In your development environment it is completely legal to have the settions of display_errors on. This is the most easy way to see if things go wrong.
In productive environment you wouldn't set this to on, because the user usually does not need to know if PHP things go wrong - he even should not, because these messages might contain valuable information for attackers. That should only be communicated through dedicated error messages like inside your die()
So the warning in your output will go away if you disable display_errors, but you really want to have this warning in your logfile. Suppressing the warning with an # is not reccommended!

Probably only going to show the warning in your development environment due to how you have error_reporting and display_errors set.
error_reporting
ini_set('display_errors', 0)
edit: Just to clarify, if your error_reporting level includes warnings in your dev environment, they can be included in errors displayed to screen. To test, set displays_errors to 0, as above right before that mysqli connect.

You can suppress the warning by calling the function prepended with #, but better to be sure to fix the source of the error.
e.g. $db = #mysqli_connect(...)

Related

MySQL error message along with PHP error message

<?php
//database connectivity
$connect_error='Sorry We could not able to connect to the database';
mysql_connect('localhost','root','') or die($connect_error);
mysql_select_db('beehive_intwebpage') or die ($connect_error);
?>
We have this in setup this in our localhost. When there no connection with the database we get the error along with the error message.
Warning: mysql_connect(): No connection could be made because the target machine actively refused it. in D:\core\database\connect.php on line 3
Sorry We could not able to connect to the database
How to show only the message and not the default sql error.
Thanks!
First: Don't use mysql_*-methods anymore, they're deprecated. Instead use mysqli or PDO.
For your error, disabling the error-reporting according to this documentation should help.
Just add error_reporting(0); to the beginning of your file.
Hey default errors are caused due to php itself, you can turn them off in the php configuration file and you have to turn them off really because watching errors give an attacker knowledge of how your site works and your sites internal structure. So just turn off the error_reporting in php configuration

How to enable error reporting in nginx php config

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.
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. –
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.
Try:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
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)

Suppress an error from the logs too

CodeIgniter 2.x still uses the classic mysql. We all know it's bad practice to still use it, but my job still requires me to use CodeIgniter.
I always have my Console.app (OSX) open watching my Apache/MySQL/PHP/CodeIgniter-native error logs.
Now I mostly have all notices/errors/etc. fixed always instantly when I see them and constantly monitor my development with Webgrind on my MAMP.
Back to the start; I constantly have one big annoying thing each page-load PHP always gives the error about mysql_pconnect is going to get deprecated in the future.
In the CodeIgniter driver the command is suppressed by # to not print the warnings to the screen, but it still ends up in my logs.
Is there any viable way to except one such error specifically in either PHP code or the PHP settings?
Locally I could recompile the whole PHP core and just remove the warning, but I would like to have the logs on our production installations rid of those warnings too :P.
Thanks in advance!
Traditionally, you can use set error verbosity using error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED) (i.e., report everything—except notices and deprecation warnings) as mentioned in "disabling deprecated errors".
Your issue may be related to CodeIgniter taking ownership of all errors.
system/core/CodeIgniter.php calls the function set_error_handler. You can find and modify the function _exception_handler it invokes on error in system/core/Common.php. It doesn't appear to be a configurable, so you may simply want to edit the line that begins with $is_error.

show error instead of "Server Error"

I have recently changed hosts, on my old host if i had an error in my syntax the error would be displayed (showing me where the error was)
On my new host i do not see this, i just see
The website encountered an error while retrieving http://www.XXX.co.uk/delete_product.php?q=66550. It may be down for maintenance or configured incorrectly.
Is there any way i can show the error instead of this?
Turn on error reporting.
Include these lines are the top of your script:
ini_set('display_errors',1);
error_reporting(E_ALL);
If you have access to edit the php.ini file, you can edit it and include the following option:
error_reporting = E_ALL
These settings will help you troubleshoot code faster and makes it easy to identify errors. However, it is not appropriate for a production-level use. You should use the first method and then you can remove the lines once you've fixed the issues. On local development environments, it's okay to edit php.ini file and add the directive as mentioned above.
On production systems, do not use ini_set('display_errors', 1); as it can show information you might want to keep hidden. Use the server's logs instead. By default apache for example logs these errors in error_log.
And, anything that is open to the general internet public is considered "production" in my opinion. Development means it is a server sitting in your own local network.
Turning on error reporting would work, but perhaps it would be better to look into the server logs.

Can't silence imap_open error notices in PHP

I am using PHP 5.3.5 and I am using
$this->marubox=#imap_open($this->server,$this->username,$this->password);
The # sign should silence error reporting but it doesnt and I am sure that the error occurs on this line. I want my application to recognize the problem itself and react and get no NOTICE errors and I can't turn off error reporting for whole PHP because of my company development policy.
Without # i am getting:
imap_open() [function.imap-open]: Couldn't open stream {pop3.seznam.cz:110/pop3}INBOX
With it i get: Notice Unknown: Authentication failed (Authentication failed) (errflg=1)
If the login information is ok it opens the connection and no errors occur.
I always get NOTICE error when imap_open doesnt manage to connect and it's messing up with my JSON results. How to silence it please?
I added
$this->marubox=#imap_open($this->server,$this->username,$this->password);
imap_errors();
imap_alerts();
and imap_errors(); and imap_alerts(); do the magic :)
Two possibilities come to mind:
You could set error_reporting in your php.ini, ini_set or .htaccess or similar so that the NOTICE is suppressed, but since you wan't your application to handle the error, this is probably not, what you need
Implement your own error handling. This is not so difficult to do. You define a function for error hadnling and then tell PHP to use it instead of it's own default handler.
//define
function myHandler($errno, $errstr) {}
//somewhere towards the beginning of your processing script
set_error_handler("myHandler");
See set_error_handler for more. Also note that from the moment you register the handler, you're solely responsible. You can suppress or throw any errors you need/want.

Categories