Online computer courses, code, programming tutorial and sidebar information for monitoring Canadian S&P/TSX index. Build friendship and networking. Welcome to visit my blogs often!!! I also have two other sites: YouTube Channel and Google site.
Adsense
Popular Posts
- PHPWind-- A PHP forum script applcaition in China
- How to blend adsense inside your post?
- Formatting my post
- Notepad++ - Add C++ compiler
- Install PHPMailer 5.2.4 and use smtp gmail
- Wayback Machine - see archived versions of web pages across time
- Set up a child account and set screen time limit in Windows 8
- phpexcel toggle expand and hide column in EXCEL and summary
- Install PHP ibm_db2 extension in Linux (redHat)
- PHP: add a download as pdf file button in report page
Thursday, March 21, 2013
MySQL transaction log in PHP
We may need to create a log table for recording MySQL transaction in PHP.
Below is the log table edu_admin_log
CREATE TABLE edu_admin_log (
`login` varchar(30) NOT NULL default '',
`time` datetime NOT NULL default '0000-00-00 00:00:00',
`operation` varchar(20) NOT NULL default '',
`table` varchar(30) NOT NULL default '',
`num_affected` tinyint(3) NOT NULL default '0',
`details` text NOT NULL,
KEY `login` (`login`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here 'details' is sql query. Operation is 1,2,3,4 or 5, corresponding to update, delete, insert, replace and other action.
Create write_to_log function to record MySQL transaction:
function write_to_log($operation_type, $table_name, $num_affected, $details) {
global $db;
static $now;
if (!$now) {
$now = date('Y-m-d H:i:s');
}
if ($num_affected > 0) {
$details = addslashes(stripslashes($details));
$sql = "INSERT INTO edu_admin_log VALUES ('$_SESSION[login]', '$now', '$operation_type', '$table_name', $num_affected, '$details')";
$result = mysql_query($sql, $db);
}
}
Usage:
$sql = "DELETE FROM edu_member_track WHERE member_id=$id";
mysql_query($sql, $db);
write_to_log(2, 'member_track', mysql_affected_rows($db), $sql);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment