Jump to content

Recommended Posts

Triggers:

A trigger is defined to activate when an INSERT, DELETE, or UPDATE statement executes for the associated table. A trigger can be set to activate either before or after the triggering statement. For example, you can have a trigger activate before each row that is inserted into a table or after each row that is updated

(http://dev.mysql.com/doc/refman/5.0/en/triggers.html)

 

But you will have to define a trigger for every table you want to keep a log for..

 

(i think those guys here: http://www.hotscripts.com/forums/php/56694-log-maintenance-mysql-affected-tables-log-table.html will give the same answer...  ;) )

ok, i tried

1) create a table for the logging

CREATE TABLE `LOGGING` (
  `id` bigint(20) NOT NULL auto_increment,
  `table` varchar(20) default NULL,
  `username` varchar(20) default NULL,
  `logtime` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 

 

2) create a script which will create the triggers (in this example i use my 'test' database...

#!/bin/bash
echo "DELIMITER ;;" >tmp.sql

for f in `mysqlshow test | sed -e 's/|//g' | grep -v '-'`; 
do 
echo 'CREATE TRIGGER t_'$f' AFTER INSERT ON '$f' FOR EACH ROW BEGIN insert into LOGGING (`table`, `username`, `logtime`) values ("'$f'",CURRENT_USER(), NOW()); end ;; ' >>tmp.sql ;
done
echo "DELIMITER ;" >>tmp.sql

 

3) its a quick and dirty script, so you have to delete some lines from the tmp.sql script...

 

at least the 1st three lines, and the line that whould create a trigger for the 'logging' table....

 

4) when you think its OK, you can execute this script to create the triggers in your database...

 

5) this works for INSERTS, you have to tweak this do the same for UPDATES and DELETES....

for f in `mysqlshow test | sed -e 's/|//g' | grep -v '-'`;

 

for f in `mysqlshow test | sed '1,4d;s/|//g;$d' | grep -v 'LOGGING'`;

 

this might be code....

now you dont have to delete thos 1st three lines, and the line for the 'LOGGING'-table,  anymore.. ;-)

Error occurring

 

<?php

#!/bin/bash

 

$hostname='localhost';

$un= 'root';

$pwd='';

$db='test';

$link=mysql_connect($hostname, $un, $pwd);

if(!$link)

  die('Could not connect: ' . mysql_error());

if(!mysql_select_db($db,$link))

  die('Could not link db: ' . mysql_error());

$f='ttt'; 

$d='DESCRIPTION';

 

echo "DELIMITER ;;" >tmp.sql ;

 

for f in "mysqlshow test | sed '1,4d;s/|//g;$d' | grep -v 'LOGGING'";

do

  echo "CREATE TRIGGER t_'$f' AFTER INSERT ON '$f' FOR EACH ROW BEGIN insert into LOGGING (table, username, logtime) values ('$f',CURRENT_USER(), NOW()); end ;; " >>tmp.sql ;

done

echo "DELIMITER ;" >>tmp.sql

 

?>

 

can u explain the mysqlshow command..

$f holds the table name whereas $d ??

 

giving error as Parse error: syntax error, unexpected T_STRING, expecting '(' in C:\xampp\htdocs\xampp\Prema\trig.php on line 16

 

ie., in the command..

expalin pls...

 

 

the 1st line in my script says:

#!/bin/bash

 

this indicates that this is not a PHP-script, but a linux-BASH-script.

you are trying to run the linux-script, as i it was PHP-code

 

mysqlshow test

This is a linux command (it also comes with the windows-install of MySQL), which will show all tablenames in the database 'test'

 

a solution in PHP might be better, because this is PHPfreaks...

 

select TABLE_NAME from information_schema.tables where TABLE_SCHEMA='test' ;

This MySQL statement returns all tablenames in the database 'test'.

You can write a script to loop over the result,

and create the statements to create the triggers for every name returned

a solution in PHP might be better, because this is PHPfreaks...

 

Errr... what? Actually KISS rule is to be obeyed no matter how this site is called.

 

I can't see how dynamically creating dozens of triggers is better than querying one table...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.