/    Sign up×
Community /Pin to ProfileBookmark

Error Reporting To DB

Folks

When an error occurs on prepared statements , mysqli, mysql etc. I do not want the user seeing the technical error reports so trying to dump it to db. And show the user a user friendly mssg.
Is this how I should do it:

[code]

ini_set(“display_errors”,1);
ini_set(“display_startup_errors”,1);
error_reporting(E_ALL);

if(!mysqli_stmt_prepare($stmt,$sql_count))
{
$_SESSION[‘error_report’] = __LINE__ .”n”;
$_SESSION[‘error_report’] = ‘Mysqli Error: ‘ .mysqli_error($conn) .”n”;
$_SESSION[‘error_report’] = ‘Mysqli Error Number: ‘ .mysqli_errno($conn) .”n”;
report_error($_SESSION[‘error_report’]);
die(‘Something went wrong. Please try again later!’);
}

function report_error($data=NULL)
{
$data = $_SESSION[‘error_report’];

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
$conn = mysqli_connect(“localhost”,”root”,””,”buzz”); //mysqli_connect(“server”,”user”,”password”,”db”);
//$stmt = mysqli_prepare($conn,”INSERT into error_reports(mysqli_error,mysqli_errno) VALUES(?,?)”);
$stmt = mysqli_prepare($conn,”INSERT into error_reports(issue) VALUES (?)”);
//mysqli_stmt_bind_param($stmt,”ii”,$mysqli_error,$mysqli_errno);
mysqli_stmt_bind_param($stmt,”s”,$_SESSION[‘error_report’]);
mysqli_stmt_execute($stmt);

mysqli_stmt_close($stmt);
mysqli_close($conn);
}

report_error($_SESSION[‘error_report’]);

[/code]

Tbl Col ‘issue’is not VarChar but TEXT.

Q1. Is this ok ?
A.

[code]
function report_error($data=NULL)
[/code]

Or should I change it to:
B.

[code]
function report_error($data)
[/code]

Or should I just leave it empty, like so:
C.

[code]
function report_error()
[/code]

I tried many times learning all that try, catchall stuff but it just does not sink in.
So, trying a very basic error recording to db.

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @nogdog

Which one should I go for ?

A ?

B ?

C ?

Unable to test as I do not know how to generate a mysqli_stmt error.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm

Can you show me how to make a mysqli_stmt serious error somewhere so I get the error spitted out on mysqli_stmt_error() or mysqli_stmt_errno()?

That way, I can always test things with the error equation for myself. And not bother people here if a certain line is correct or not as I can always test it to see if I get the errors or not. Right now, I do not know how to generate an error on any of the the mysqli_stmt() functions apart from mysqli_stmt_connect() function as I know how to screw things up on that one (write the db name or password or user name wrong) to generate a mysqli_stmt_connect_error() or mysqli_stmt_connect_errno() for that one.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @nogdog

Which one should I go for ?

A ?

B ?

C ?

Unable to test as I do not know how to generate a mysqli_stmt error.
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — I help people who have real problems. Not people who have the time to create worthless pieces of code that they are not even using and want to know the best way to use them. If it works what else matters? Put it to use and test it. That's what real programmers do in life.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646070

You know what the problem is ? I been testing for halfen hr now.

Now tell me, are not these 2 codes supposed to generate error codes or not ?

I deliberately typed the column name wrong so an error gets generated as testing if error gets successfully dumped or not to mysql db for Admin to examin,

A.
<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

<i> </i>$sql = "INSERT into domains (domain,domain_emails,password) VALUES (?,?,?)";

<i> </i>if(!mysqli_stmt_prepare($stmt,$sql))
<i> </i>{
<i> </i> $_SESSION['error_report'] .= __LINE__ ."n";
<i> </i> $_SESSION['error_report'] .= 'Mysqli Statement Error: ' .mysqli_stmt_error($stmt) ."n";
<i> </i> $_SESSION['error_report'] .= 'Mysqli Statement Error Number: ' .mysqli_stmt_errno($stmt) ."n";
<i> </i> report_error($_SESSION['error_report']);
<i> </i> die('3. Something went wrong. Please try again later!');


B.
<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

<i> </i>$sql = "INSERT into domains (domain,domain_emails,password) VALUES (?,?,?)";

<i> </i>if(!mysqli_stmt_prepare($stmt,$sql))
<i> </i>{
<i> </i> $_SESSION['error_report'] .= __LINE__ ."n";
<i> </i> $_SESSION['error_report'] .= 'Mysqli Statement Error: ' .mysqli_stmt_error($stmt) ."n";
<i> </i> $_SESSION['error_report'] .= 'Mysqli Statement Error Number: ' .mysqli_stmt_errno($stmt) ."n";
<i> </i> report_error($_SESSION['error_report']);
<i> </i> die('3. Something went wrong. Please try again later!');


Guess what error codes they generate ? Blank!

This is what I see getting dumped in my db (3 rows):

**174

Mysqli Statement Error

Mysqli Statement Error Number**


NOTE 1: No Error Details or Number gets dumped to db.

NOTE 2: 174 is the line number (__LINE__).

My report error function, that is supposed to dump error code to db, looks like this, incase you wondering:
[code]
function report_error($data=NULL)
{
mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$conn = mysqli_connect("localhost","root","","buzz"); //mysqli_connect("server","user","password","db");
$data = $_SESSION['error_report'];

$stmt = mysqli_prepare($conn,"INSERT into error_reports(issue) VALUES (?)");
mysqli_stmt_bind_param($stmt,"s",$data);
mysqli_stmt_execute($stmt);

mysqli_stmt_close($stmt);
mysqli_close($conn);
}
[code]
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — See my previous post.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646073

But the above is not worthless piece of code as I am trying to get the code to not show error to user but dump it to db for Admin to check.
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — Your error reporting function is totally incorrect.

1 - why make a new connection when you already have one?

2 - why do a prepare when your system is producing the data for the query and not the user?

3 - why pass in a parm to the function when you don't use it?
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646077

Thanks a bunch!

Now that is what is called an eye of an eagle. Real programmer. And not a coder (cut & paster).

I will get back to you after fixing the code I screwed up! :)
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646077

Actually, I removed the mysql connection line about halfen hr ago from my report_error() function. You were seeing old code from nearly an hr back.

Current code looks like this:
<i>
</i>ini_set("display_errors",1);
ini_set("display_startup_errors",1);
error_reporting(E_ALL);

function report_error($data=NULL)
{
$data = $_SESSION['error_report'];

<i> </i>$stmt = mysqli_prepare($conn,"INSERT into error_reports(issue) VALUES (?)");
<i> </i>mysqli_stmt_bind_param($stmt,"s",$data);
<i> </i>mysqli_stmt_execute($stmt);
<i> </i>
<i> </i>mysqli_stmt_close($stmt);
<i> </i>mysqli_close($conn);
}

report_error($_SESSION['error_report']);


As for $data variable, I am making use of it.

As for why I did a prepared statement to dump error code to db for Admin to read. Well, after getting into prepared statement, I have now forgotten how to do it the non-prepareed way now. I will now try recalling back memory and fixing it without copy pasting from somewhere.
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — That code is so wrong. And not using the parm that is passed in since you overwrite it immediately.
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646077

You know what the funny thing is G ?

While trying to unprepare statement, I wrote this typo (note the extra closing brace at the end):
<i>
</i>$sql_report_error = "INSERT into error_reports(issue) $data);

And guess what ? I got no error and data was dumped to db!

And even this worked:
<i>
</i>$sql_report_error = "INSERT into error_reports(issue) $data;

NOTE no closing dbl quote on both codes. Not sure what is going on here.

My final code looks like this ....
<i>
</i>ini_set("display_errors",1);
ini_set("display_startup_errors",1);
error_reporting(E_ALL);

function report_error($data=NULL)
{
$data = $_SESSION['error_report'];

<i> </i>$sql_report_error = "INSERT into error_reports(issue) $data";
}

report_error($_SESSION['error_report']);


If you think that is wrong then care to remind me how to write code the non-prepared way ?
Copy linkTweet thisAlerts:
@ginerjmAug 19.2022 — You are not using the parm that is passed in since you overwrite it immediately.

You need to pass in your connection.

You didn't read the rest of my previous post so why am I even looking at this?
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm#1646084

>>You need to pass in your connection.<<

I had connection code inside the report_error() function.


But did not you say....
>>1 - why make a new connection when you already have one?<<

And so, I ditched it from inside the function as connection is already called before the report_error() function gets called.


Yeah, I missed your previous post cos we were posting at same time.

I think I read in some tutorial that it is safer to write like this:
<i>
</i>report_error($data=NULL);

I can't remember why it was safe to do so. I guess you want me to remove it. Ok then.

Here you go ...
<i>
</i>function report_error($data)
{
$data = $_SESSION['error_report'];

<i> </i>$sql_report_error = "INSERT into error_reports(issue) VALUES($data)";
}

report_error($_SESSION['error_report']);


Is it ok now ?

And here is the context ....

A.
<i>
</i>if(!mysqli_stmt_prepare($stmt,$sql))
{
echo __LINE__; echo '&lt;br&gt;';

<i> </i>$_SESSION['error_report'] .= __LINE__ ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Statement Error: ' .mysqli_stmt_error($stmt) ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Statement Error Number: ' .mysqli_stmt_errno($stmt) ."n";
<i> </i>report_error($_SESSION['error_report']);
<i> </i>die('3. Something went wrong. Please try again later!');


B.
<i>
</i>if(!mysqli_stmt_prepare($stmt,$sql))
{
echo __LINE__; echo '&lt;br&gt;';

<i> </i>$_SESSION['error_report'] .= __LINE__ ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Error: ' .mysqli_error($stmt) ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Error Number: ' .mysqli_errno($stmt) ."n";
<i> </i>report_error($_SESSION['error_report']);
<i> </i>die('3. Something went wrong. Please try again later!');


WHich one should I stick to ? Logic says B.

But if I was using prepared statement then I should use A.

Right Gin ?
Copy linkTweet thisAlerts:
@novice2022authorAug 19.2022 — @ginerjm

Why do I get the feeling this is not what you want:
<i>
</i>function report_error($data)
{
$data = $_SESSION['error_report'];

$sql_report_error = "INSERT into error_reports(issue) VALUES($data)";
}

report_error($_SESSION['error_report']);


But this is what you want:
<i>
</i>function report_error()
{
$data = $_SESSION['error_report'];

$sql_report_error = "INSERT into error_reports(issue) VALUES($data)";
}

report_error($_SESSION['error_report']);


Correct ?
Copy linkTweet thisAlerts:
@ginerjmAug 20.2022 — You really have no idea what you are doing here, do you?
Copy linkTweet thisAlerts:
@novice2022authorAug 20.2022 — @ginerjm#1646088

I do.

I had sleep in my brain, if not in my eyes, last night as I was bugging you till 4am here.

The first code is buggy. Why ? Let me explain then you can tell me if I am correct or not. Might aswell make use of your skills while I get the chance and you're in the mood.
<i>
</i>function report_error($data)
{
$data = $_SESSION['error_report'];

$sql_report_error = "INSERT into error_reports(issue) VALUES($data)";
}

report_error($_SESSION['error_report']);

Above, I am passing session data onto $data.

And then I am defining $data again. So, your question is, why am I defining twice ?

You right. Ditching this one.

Sticking to the final one.

So far, so good ?
Copy linkTweet thisAlerts:
@novice2022authorAug 20.2022 — @sempervivum

Does this look like a valid error code to you ?

I get this same 3 rows of error data dumped to my mysql tbl, no matter which of the following 2 code I use. Why is that ?

**174

Mysqli Statement Error:

Mysqli Statement Error Number: 0**


A
<i>
</i>mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

$sql = "INSERT into domains (domain,domain_emails,password) VALUES (?,?,?)";

if(!mysqli_stmt_prepare($stmt,$sql))
{
echo __LINE__; echo '&lt;br&gt;';//THIS IS LINE 174

<i> </i>$_SESSION['error_report'] .= __LINE__ ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Statement Error: ' .mysqli_stmt_error($stmt) ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Statement Error Number: ' .mysqli_stmt_errno($stmt) ."n";
<i> </i>report_error($_SESSION['error_report']);
<i> </i>die('3. Something went wrong. Please try again later!');


B
<i>
</i>if(!mysqli_stmt_prepare($stmt,$sql))
{
echo __LINE__; echo '&lt;br&gt;';//THIS IS LINE 174

<i> </i>$_SESSION['error_report'] .= __LINE__ ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Error: ' .mysqli_error($conn) ."n";
<i> </i>$_SESSION['error_report'] .= 'Mysqli Error Number: ' .mysqli_errno($conn) ."n";
<i> </i>report_error($_SESSION['error_report']);
<i> </i>die('Something went wrong. Please try again later!');


Shall I stick to mysqli_error() or mysqli_stmt_error() here ?

And why I get error code: 0.

That's the big question. I am supposed to get error here as I deliberately typed the mysql tbl col name wrong to trigger an error.
Copy linkTweet thisAlerts:
@zeinahostAug 25.2022 — أفضل شركة سيو

شركة سيو


افضل شركة سيو
×

Success!

Help @novice2022 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.17,
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,
)...