/    Sign up×
Community /Pin to ProfileBookmark

Mamangement of errors and report them

I have code like:

[code=php]
mysql_query ($query,$link)
or die (“Insert error:”.mysql_error());
mysql_close ($link);
[/code]

How to report and echo error with number that user will not notice what error is with content? How are managed errors in such cases? Maybe article..suggestion ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@coppocksDec 01.2006 — With some applications, I log the errors, so using your code... I apply the '@' symbol to suppress/hide the error from the user.

And then if there is an error, I've assigned an error reference ID and a date to the query so I'll know where/when it occurred...

Now this simple example is intended to give you an idea. I am sure that others do things differently or easier. In this example, I've assigned this location with a 1. If I see this error number in the error_logs, I know exactly where it occured within my application.

<i>
</i>@mysql_query ($query,$link);
if(mysql_error($link)) {
$errorTxt = mysql_errno($link) . ": " . mysql_error($link);
// Some code here to add to an error log, either an SQL table or file
@mysql_query("INSERT INTO error_logs set errorID = '1', errorDate= 'time()', errorTxt = '$errorTxt'");
}


In practice, I might have a function to handle errors.

<i>
</i>function error_logger($eID,$er) {
$eTime = time();
@mysql_query("INSERT INTO error_logs set errorID = '$eID', errorDate= '$eTime', errorTxt = '$er'");
}


In that case, your code would be...

<i>
</i>@mysql_query ($query,$link);
if(mysql_error($link)) {
$errorID = '1';
$errorTxt = mysql_errno($link) . ": " . mysql_error($link);
error_logger($errorID,$errorTxt);
}
Copy linkTweet thisAlerts:
@toplisekauthorDec 05.2006 — What will do code sentence:
[code=php]
$errorTxt = mysql_errno($link) . ": " . mysql_error($link);
[/code]
Copy linkTweet thisAlerts:
@coppocksDec 05.2006 — If there is a MySQL defined error, mysql_errno() returns the error number and mysql_error() returns the text of the error message from previous MySQL operation.

That line creates a text string that might look like this example:

1049: Unknown database 'nonexistentdb'
Copy linkTweet thisAlerts:
@toplisekauthorDec 05.2006 — If I understand this code will work? Need help. ?
[code=php]
@mysql_query ($query,$link);
if(mysql_error($link)) {
$errorID = '1';
$errorTxt = mysql_errno($link) . ": " . mysql_error($link);
error_logger($errorID,$errorTxt);

function error_logger($date,$time, $errorID,$errortext)

{
/* Connect to MySQL-Server */
$db=DB_DATABASE;
$link=mysql_connect (DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD);
if (!$link)
{
die ("Couldn't connect to MySQL:". mysql_error());
}


/* Open $db */
mysql_select_db ("$db")
or die ("Couldn't open $db: ". mysql_error());

$time= date("H:i:s", mktime(date("h")-6, date("i"), date("s"), date("n"), date("j"), date("Y")));
$date= date("Y-m-d", mktime(date("h"), date("i"), date("s"), date("n"), date("j"), date("Y")));


@mysql_query = "insert into errors_logs
values ('$date','$time', '$errorID','$errortext')";
}
[/code]
×

Success!

Help @toplisek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...