PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > How to get error context
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to get error context

Réponse
 
LinkBack Outils de la discussion
Vieux 13/03/2008, 21h28   #1
It Maq
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to get error context

Hi,

i need because I'm unable to retrieve error's context. Below is the code that is working perfectly except the context that is not displayed:

<?php
function error_handler($errno, $errstr, $filename, $lineno, $errcontext)
{


$str = "";
$str .= "<br><br>ERROR Number: ". $errno;
$str .= "<br>ERROR MESSAGE: ". $errstr;
$str.= "<br>File: ". $filename;
$str.= "<br>Line number: ". $lineno;
$str.= "<br>Context: ". $errcontext[0];
echo $str;


}

echo "<h1>Testing set_error_handler</h1>";

error_reporting(0);
set_error_handler('error_handler');
ini_set('error_reporting', 0);

//trigger_error("The string for testing the error", E_USER_WARNING);
$ourFileName = "testFile.txt";
$fh = fopen($ourFileName, 'X');// or die("Can't open file");
fclose($fh);


?>




__________________________________________________ __________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ


  Réponse avec citation
Vieux 13/03/2008, 23h45   #2
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to get error context

On Mar 13, 3:28 pm, itmaqu...@yahoo.com (It Maq) wrote:
> Hi,
>
> i need because I'm unable to retrieve error's context. Below is the code that is working perfectly except the context that is not displayed:
>
> <?php
> function error_handler($errno, $errstr, $filename, $lineno, $errcontext)
> {
>
> $str = "";
> $str .= "<br><br>ERROR Number: ". $errno;
> $str .= "<br>ERROR MESSAGE: ". $errstr;
> $str.= "<br>File: ". $filename;
> $str.= "<br>Line number: ". $lineno;
> $str.= "<br>Context: ". $errcontext[0];
> echo $str;
>
> }
>
> echo "<h1>Testing set_error_handler</h1>";
>
> error_reporting(0);
> set_error_handler('error_handler');
> ini_set('error_reporting', 0);
>
> //trigger_error("The string for testing the error", E_USER_WARNING);
> $ourFileName = "testFile.txt";
> $fh = fopen($ourFileName, 'X');// or die("Can't open file");
> fclose($fh);
>
> ?>
>
> __________________________________________________ __________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ



Do a print_r() on $errcontext and you'll find that it isn't
numerically indexed. As such there is no value at $errcontext[0].
The keys of that array are the names of the global variables and the
values they point to are those variables' values.

For instance, $errcontext['ourFileName'] contains the value
testFile.txt because that's what it was set to when the error occurred.
  Réponse avec citation
Vieux 14/03/2008, 00h55   #3
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context

It Maq wrote:
> Hi,
>
> i need because I'm unable to retrieve error's context. Below is the code that is working perfectly except the context that is not displayed:
>
> <?php
> function error_handler($errno, $errstr, $filename, $lineno, $errcontext)
> {
>


try doing a print_r($errcontext) here and see what you get?

maybe the zero index does not exist.

>
> $str = "";
> $str .= "<br><br>ERROR Number: ". $errno;
> $str .= "<br>ERROR MESSAGE: ". $errstr;
> $str.= "<br>File: ". $filename;
> $str.= "<br>Line number: ". $lineno;
> $str.= "<br>Context: ". $errcontext[0];
> echo $str;
>
>
> }
>
> echo "<h1>Testing set_error_handler</h1>";
>


I have always used
error_reporting(E_ALL);
instead of this.
> error_reporting(0);
> set_error_handler('error_handler');
> ini_set('error_reporting', 0);


I have never seen the above line before. This and the error_reporting(0) would
probably suppress all error messages generated by PHP. Double check these
settings and make sure the you can still trigger an error with trigger_error()
if you have error_reporting() turned off.

>
> //trigger_error("The string for testing the error", E_USER_WARNING);
> $ourFileName = "testFile.txt";
> $fh = fopen($ourFileName, 'X');// or die("Can't open file");
> fclose($fh);
>
>
> ?>
>
>
>
>
> __________________________________________________ __________________________________
> Be a better friend, newshound, and
> know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ
>
>
>



--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare
  Réponse avec citation
Vieux 14/03/2008, 16h20   #4
It Maq
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context

Hi,

I maid the modifications you suggested. For the error
context when i display it, it gives a lot of
information, but not easy to understand how the array
is structured. If somebody knows about a site
explaining how to access the context it would be
great.
I also modified the error reporting to E_ALL as you
suggested. Now an other question arise: For the
connection to the database if i put a wrong username
or password the error is reported automatically to the
function that i defined as the error handling
function, but for the next statement (see code below)
if i put a wrong database name the error is not
reported automatically, i have to trigger it using
triger_error. So i'm wondering if there are some rules
that can me know if an error will be reported
automatically or not. here is my code after
modification:

<?php
function error_handler($errno, $errstr, $filename,
$lineno, $errcontext)
{
echo "handling reported error<br>";

switch($errno) {
case E_USER_ERROR: $type = "E_USER_ERROR";break;
case E_USER_WARNING: $type =
"E_USER_WARNING";break;
case E_USER_NOTICE: $type = "E_USER_NOTICE";break;
case E_ERROR: $type = "E_ERROR";break;
case E_WARNING: $type = "E_WARNING";break;
case E_PARSE: $type = "E_PARSE";break;
case E_NOTICE: $type = "E_NOTICE";break;
case E_CORE_ERROR: $type = "E_CORE_ERROR";break;
case E_CORE_WARNING: $type =
"E_CORE_WARNING";break;
case E_COMPILE_ERROR: $type =
"E_COMPILE_ERROR";break;
case E_STRICT: $type = "E_STRICT";break;
case E_COMPILE_WARNING: $type =
"E_COMPILE_WARNING";break;
case E_RECOVERABLE_ERROR: $type =
"E_RECOVERABLE_ERROR";break;
default: echo "<br>This is an error not listed";

}
$str = "";
$str .= "<br><br>ERROR TYPE: ". $type;
$str .= "<br><br>ERROR Number: ". $errno;
$str .= "<br>ERROR MESSAGE: ". $errstr;
$str.= "<br>File: ". $filename;
$str.= "<br>Line number: ". $lineno;
echo $str;
echo "Context: <br>";
print_r($errcontext);
}





echo "<h1>Testing set_error_handler</h1>";

error_reporting(E_ALL);
set_error_handler('error_handler');
mysql_connect("localhost", "admin", "admin");
mysql_select_db("wrongdb") or trigger_error("wrong
database");

?>
--- Jim Lucas <lists@cmsws.com> wrote:

> It Maq wrote:
> > Hi,
> >
> > i need because I'm unable to retrieve error's

> context. Below is the code that is working perfectly
> except the context that is not displayed:
> >
> > <?php
> > function error_handler($errno, $errstr,

> $filename, $lineno, $errcontext)
> > {
> >

>
> try doing a print_r($errcontext) here and see what
> you get?
>
> maybe the zero index does not exist.
>
> >
> > $str = "";
> > $str .= "<br><br>ERROR Number: ". $errno;
> > $str .= "<br>ERROR MESSAGE: ". $errstr;
> > $str.= "<br>File: ". $filename;
> > $str.= "<br>Line number: ". $lineno;
> > $str.= "<br>Context: ". $errcontext[0];
> > echo $str;
> >
> >
> > }
> >
> > echo "<h1>Testing set_error_handler</h1>";
> >

>
> I have always used
> error_reporting(E_ALL);
> instead of this.
> > error_reporting(0);
> > set_error_handler('error_handler');
> > ini_set('error_reporting', 0);

>
> I have never seen the above line before. This and
> the error_reporting(0) would
> probably suppress all error messages generated by
> PHP. Double check these
> settings and make sure the you can still trigger an
> error with trigger_error()
> if you have error_reporting() turned off.
>
> >
> > //trigger_error("The string for testing the

> error", E_USER_WARNING);
> > $ourFileName = "testFile.txt";
> > $fh = fopen($ourFileName, 'X');// or die("Can't

> open file");
> > fclose($fh);
> >
> >
> > ?>




__________________________________________________ __________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ


  Réponse avec citation
Vieux 14/03/2008, 16h28   #5
Zoltán Németh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context

2008. 03. 14, péntek keltezéssel 07.20-kor It Maq ezt Ãrta:
> So i'm wondering if there are some rules
> that can me know if an error will be reported
> automatically or not.


there is no general rule for that. you have to check the manual for each
function, some of them just returns false on error, others throw
warnings/notices/errors...

greets,
Zoltán Németh

  Réponse avec citation
Vieux 14/03/2008, 16h40   #6
It Maq
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context

For example "mysql_connect" reprted automatically the
error but in the manual
http://us3.php.net/manual/en/function.mysql-connect.php
all they give as information is the return: "Returns
a MySQL link identifier on success, or FALSE on
failure.", where can i see if it throws an error, and
when you say throwing do you mean that i can catch the
error without throwing it myself?

--- Zoltán Németh <znemeth@alterationx.hu> wrote:

> 2008. 03. 14, péntek keltezéssel 07.20-kor It Maq
> ezt írta:
> > So i'm wondering if there are some rules
> > that can me know if an error will be reported
> > automatically or not.

>
> there is no general rule for that. you have to check
> the manual for each
> function, some of them just returns false on error,
> others throw
> warnings/notices/errors...
>
> greets,
> Zoltán Németh
>
>




__________________________________________________ __________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs

  Réponse avec citation
Vieux 14/03/2008, 17h13   #7
Zoltán Németh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context

2008. 03. 14, péntek keltezéssel 07.40-kor It Maq ezt Ãrta:
> For example "mysql_connect" reprted automatically the
> error but in the manual
> http://us3.php.net/manual/en/function.mysql-connect.php
> all they give as information is the return: "Returns
> a MySQL link identifier on success, or FALSE on
> failure.", where can i see if it throws an error, and
> when you say throwing do you mean that i can catch the
> error without throwing it myself?


hmm actually what error did mysql_connect throw?
because if it just fails connecting, it returns false. on the other
hand, if you provide it wrong arguments (e.g. less arguments, or wrong
data type, or whatever) that raises a php error and the function does
not even run.
this is true for most functions which return false on error. the
returning false means there was some error with the action itself, while
php errors are raised when the action can not be executed because of
some error in the code itself.

greets,
Zoltán Németh

>
> --- Zoltán Németh <znemeth@alterationx.hu> wrote:
>
> > 2008. 03. 14, péntek keltezéssel 07.20-kor It Maq
> > ezt Ãrta:
> > > So i'm wondering if there are some rules
> > > that can me know if an error will be reported
> > > automatically or not.

> >
> > there is no general rule for that. you have to check
> > the manual for each
> > function, some of them just returns false on error,
> > others throw
> > warnings/notices/errors...
> >
> > greets,
> > Zoltán Németh
> >
> >

>
>
>
> __________________________________________________ __________________________________
> Never miss a thing. Make Yahoo your home page.
> http://www.yahoo.com/r/hs
>
>


  Réponse avec citation
Vieux 14/03/2008, 19h03   #8
It Maq
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context

Here is the error message captured by my error
handling function:

mysql_connect() [function.mysql-connect]: Access
denied for user 'admin'@'localhost' (using password:
YES)

i put a wrong password and username
--- Zoltán Németh <znemeth@alterationx.hu> wrote:

> 2008. 03. 14, péntek keltezéssel 07.40-kor It Maq
> ezt írta:
> > For example "mysql_connect" reprted automatically

> the
> > error but in the manual
> >

>

http://us3.php.net/manual/en/function.mysql-connect.php
> > all they give as information is the return:

> "Returns
> > a MySQL link identifier on success, or FALSE on
> > failure.", where can i see if it throws an error,

> and
> > when you say throwing do you mean that i can catch

> the
> > error without throwing it myself?

>
> hmm actually what error did mysql_connect throw?
> because if it just fails connecting, it returns
> false. on the other
> hand, if you provide it wrong arguments (e.g. less
> arguments, or wrong
> data type, or whatever) that raises a php error and
> the function does
> not even run.
> this is true for most functions which return false
> on error. the
> returning false means there was some error with the
> action itself, while
> php errors are raised when the action can not be
> executed because of
> some error in the code itself.
>
> greets,
> Zoltán Németh
>
> >
> > --- Zoltán Németh <znemeth@alterationx.hu> wrote:
> >
> > > 2008. 03. 14, péntek keltezéssel 07.20-kor It

> Maq
> > > ezt írta:
> > > > So i'm wondering if there are some rules
> > > > that can me know if an error will be

> reported
> > > > automatically or not.
> > >
> > > there is no general rule for that. you have to

> check
> > > the manual for each
> > > function, some of them just returns false on

> error,
> > > others throw
> > > warnings/notices/errors...
> > >
> > > greets,
> > > Zoltán Németh
> > >
> > >

> >
> >
> >
> >

>

__________________________________________________ __________________________________
> > Never miss a thing. Make Yahoo your home page.
> > http://www.yahoo.com/r/hs
> >
> >

>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>




__________________________________________________ __________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping

  Réponse avec citation
Vieux 17/03/2008, 11h59   #9
Zoltán Németh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] How to get error context


2008. 03. 14, péntek keltezéssel 10.03-kor It Maq ezt Ãrta:
> Here is the error message captured by my error
> handling function:
>
> mysql_connect() [function.mysql-connect]: Access
> denied for user 'admin'@'localhost' (using password:
> YES)
>
> i put a wrong password and username


the manual also says this, which I too did not notice earlier:

"Note: You can suppress the error message on failure by prepending a @
to the function name."

this implies that error message is raised on error, but don't ask me
why

greets,
Zoltán Németh

> --- Zoltán Németh <znemeth@alterationx.hu> wrote:
>
> > 2008. 03. 14, péntek keltezéssel 07.40-kor It Maq
> > ezt Ãrta:
> > > For example "mysql_connect" reprted automatically

> > the
> > > error but in the manual
> > >

> >

> http://us3.php.net/manual/en/function.mysql-connect.php
> > > all they give as information is the return:

> > "Returns
> > > a MySQL link identifier on success, or FALSE on
> > > failure.", where can i see if it throws an error,

> > and
> > > when you say throwing do you mean that i can catch

> > the
> > > error without throwing it myself?

> >
> > hmm actually what error did mysql_connect throw?
> > because if it just fails connecting, it returns
> > false. on the other
> > hand, if you provide it wrong arguments (e.g. less
> > arguments, or wrong
> > data type, or whatever) that raises a php error and
> > the function does
> > not even run.
> > this is true for most functions which return false
> > on error. the
> > returning false means there was some error with the
> > action itself, while
> > php errors are raised when the action can not be
> > executed because of
> > some error in the code itself.
> >
> > greets,
> > Zoltán Németh
> >
> > >
> > > --- Zoltán Németh <znemeth@alterationx.hu> wrote:
> > >
> > > > 2008. 03. 14, péntek keltezéssel 07.20-kor It

> > Maq
> > > > ezt Ãrta:
> > > > > So i'm wondering if there are some rules
> > > > > that can me know if an error will be

> > reported
> > > > > automatically or not.
> > > >
> > > > there is no general rule for that. you have to

> > check
> > > > the manual for each
> > > > function, some of them just returns false on

> > error,
> > > > others throw
> > > > warnings/notices/errors...
> > > >
> > > > greets,
> > > > Zoltán Németh
> > > >
> > > >
> > >
> > >
> > >
> > >

> >

> __________________________________________________ __________________________________
> > > Never miss a thing. Make Yahoo your home page.
> > > http://www.yahoo.com/r/hs
> > >
> > >

> >
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >

>
>
>
> __________________________________________________ __________________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h55.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21867 seconds with 17 queries