数据库中存储错误的错误函数

i have this function that i use to handle my errors
within php query and things like that

but i thought it will be better that if i can save the errors in the database then show it up in my admin control panel, so no one can see the errors that i have on my website

here is the function :

function er($str) {
global $db;
if (!$str) {
$errornum = $db->errorinfo();
echo $errornum[2];
die;
  }
}

and here i was trying to do what i said i want to do :

function er($str) {
global $db;
if (!$str) {
$errornum = $db->errorinfo();
$en = $errornum[2];
echo "".$en."";
$db->query("INSERT INTO `error` (`con`) VALUES ('$en')");
die;
  }
}

The Problem Is :   

that the query dosent do anything

The Reason Is :

i believe that the reason is that $en is empty ... so the insert wont do anything as it dosent have a name to insert it into the database , but $en isnt empty as when i echo it i dose output the error .. i fugure that out while i was trying to get it work i replaced $en with (Example) and it worked and (Example) was there in the database

Fix :

I'm not sure but i think if i can get it to paste the real value of the $en .. then it will be working :D

But i need you to help me to do it or point me to the right direction

Well.

What you have to get rid of:

  • no die()
  • no errors echoed to browser
  • no database logging

What you really have to do:

this way you will see all your errors in cpanel and none of them will be spat to the user.

$db->query("INSERT INTO `error` (`con`) VALUES ('$en')");

instead try

$db->query("INSERT INTO `error` (`con`) VALUES ('".$en."') )";

If its echoing the error I doubt that $en is empty so its probably just a string interpolation issue.