将错误保存到MySQL数据库 [英] Save error to MySQL database

查看:90
本文介绍了将错误保存到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php查询来更新MySQL数据库,请参见下文

I have a php query to update a MySQL database, see below

$sql=("update hr_payroll set 
payroll_number='$payroll_number', 
tax_code='$tax',
bacs_ref='$bacs_ref',  
pay_frequency='$pay',
last_update_by='$user'
where employee_id='$employee'")or die('Could not connect: Error 23 ' . mysql_error());

如果此查询成功,将在数据库中插入一行,请参见下文;

If this query is successful a row is inserted into the DB, see below;

if ($conn->query($sql) === TRUE) {
$sql1 = "INSERT INTO audit_hr_employees
(tab, employee, error_type, user, result)
VALUES ('4a', '$employee', 'info', '$user', 'success')";
}

这一切正常....但是,如果查询失败,我想做与上述相同的操作,但是将错误或查询添加到数据库中.

This all works fine.... However if the query is not successful I want to do the same as above but add the error or the query into the database.

下面是我的代码段,我希望将信息添加到error_info列中.

Below is a snippet of my code, I want the information to be added to the error_info column.

else
{       
$sql2 = "INSERT INTO audit_hr_employees 
(tab, employee, error_type, user, error_info)
VALUES ('4a', '$employee', 'warning', '$user', '  ')";
}

这可能吗?

推荐答案

似乎您正在通过PHP的PDO接口连接到MySQL.您可以使用errorInfo()函数( http://php.net/manual/zh/pdo.errorinfo.php )来检索最新的错误消息,并使用它代替您的空白字符串:

It looks like you're connecting to MySQL via PHP's PDO interface. You can use the errorInfo() function (http://php.net/manual/en/pdo.errorinfo.php) to retrieve the most recent error message and use that in place of your blank string:

$err = $dbh->errorInfo();

$sql2 = "INSERT INTO audit_hr_employees 
(tab, employee, error_type, user, error_info)
VALUES ('4a', '$employee', 'warning', '$user', $err[2])";

这篇关于将错误保存到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆