Reputation: 193
I wrote the following snippet in order to handle errors.
(Page.php gets included in index page through : array( falseNamePage=> page.php, ....ect))
Perfoming some tests to see how it reacts, I delete a page.php from public_html.
RESULT :
-error logging ok
-alert email sending ok
-recording in DB : ERROR :
Notice: Undefined variable: $database in /home/.../public_html/index.php on line 40
Warning: mysql_query() expects parameter 2 to be resource, null given in /home/.../public_html/index.php on line 40 "impossible to connect with DB2"
I do not understands why it fails connecting to the DB in this case and sends back an error.
DB Connection works fine in every other cases ( delete, select,update, insert ...)
function errorHandler($errno, $errstr, $errfile, $errline)
{
require_once('connection.php');
$now = time();
$date = date("Y-m-d H:i:s",$now);
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_STRICT:
............ 5 first cases code...............
case E_WARNING:
case E_USER_WARNING:
$message_warning = "Warning : ".$errno." : ".$errstr." : ".$errfile." : ".$errline;
error_log ( $message_warning ,0);
$mail = '[email protected]'; $sujet = $message_warning; $body_warning = $date." : ".$message_warning;
mail($mail,'=?UTF-8?B?'.base64_encode($sujet).'?=',stripslashes($body_warning));
$query_warning =" INSERT INTO errorlog (severity,errno,errstr,errfile,errline,time)
VALUES ('WARNING','".$errno."','".$errstr."','".$errfile."','".$errline."','".$date."')";
$result_warning = mysql_query($query_warning,$database) or die("impossible to connect with DB2");
break;
case E_ERROR:
case E_USER_ERROR:
............... 2 last cases code ..........
}
}
set_error_handler("errorHandler");
The final question is :
WHY IS AN INCLUDE ERROR ECHOED 4 TIMES ?
Does the system attempts 4 times to "open stream"?
I did :
function errorHandler($errno, $errstr, $errfile, $errline)
{
if ($errno == E_NOTICE )
{ echo "<br/>".$errno."== E_NOTICE<br/>";}
if ($errno == E_USER_NOTICE)
{ echo "<br/>".$errno."== E_USER_NOTICE<br/>";}
if ($errno == E_DEPRECATED)
{ echo "<br/>".$errno."== E_DEPRECATED<br/>";}
if ($errno == E_USER_DEPRECATED)
{ echo "<br/>".$errno."== E_USER_DEPRECATED<br/>";}
if ($errno == E_STRICT)
{ echo "<br/>".$errno."== E_STRICT<br/>";}
if ($errno == E_WARNING)
{ echo "<br/>".$errno."== E_WARNING<br/>";}
if ($errno == E_USER_WARNING)
{ echo "<br/>".$errno."== E_USER_WARNING<br/>";}
if ($errno == E_ERROR)
{ echo "<br/>".$errno."== E_ERROR<br/>";}
if ($errno == E_USER_ERROR)
{ echo "<br/>".$errno."== E_USER_ERROR<br/>";}
}
set_error_handler("errorHandler");
RESULT :
2== E_WARNING
2== E_WARNING
2== E_WARNING
2== E_WARNING
Upvotes: 0
Views: 229
Reputation: 106385
Your problem is simple, and, in my opinion, it's YAPHPB: you're using require_once
within a function definition, hoping that the file will be included just once - to shape up a function's body.
But it doesn't work like that: this code will be parsed each time (well, it's a bit simplified, but the reasons still the same) the function will get called. And because it's require_once
, your file will be included just once indeed - only when the function is called first time. All the next calls will skip the file's inclusion, hence $database will not be defined.
The easiest way to fix it is to replace require_once
with require
. But I think the problem just will be covered - but not solved. The real solution would be to restructure your code in such way that your $database
will actually become a record in registry - and will be lazily extracted from that registry if needed.
There are many ways to implement such behaviour: we often use Zend_Registry component for that. And in this topic there are some examples of how to use this component effectively.
Upvotes: 0
Reputation: 20492
Probably connection.php
has already been included before, so when you use require_once
in your code, it does not include connection.php
again. Then, $database
variable will not be defined, and you will got that error message.
Upvotes: 1
Reputation: 41050
When there is no function definition and no constant definition in connection.php
use require
instead of require_once
.
The variable $database will not be available when the function is called the second time, because of require_once. It is a little bit bad designed here.
A solution would be to have $database as a constant, so it acts like a global variable no matter if connection.php was the included first or second time.
Upvotes: 0