PHP pdo,如何将执行到数据库的每个查询作为日志存储到同一数据库中的表

PHP pdo,如何将执行到数据库的每个查询作为日志存储到同一数据库中的表

问题描述:

working with php5.5 and mysql5.5, have developed an application with the PDO connection. now as log table I want to create a table(db_log):

id--------auto
query ----query 
create----curent time
user------session-user

AND store every insert, update and delete action on database to a mention table, for example there table named( tbl_temp) with some coumns and there user come and run query like

(DELETE * FROM tbl_temp where id = 1) from the user (user1)

here when this query runs like from the page of (delete.php).. it should save the query on the table of db_log

id = 1
query = DELETE * FROM tbl_temp where id = 1
create = datetime
user = user1

so that how i will be able to record every action of user on database and control the user activity,

1 - here do i need to pass the query to db_log in every, page, or i can build a class 2- is there any good solution or example on web to learn.

使用php5.5和mysql5.5,开发了一个带有PDO连接的应用程序。 now as log table 我想创建一个表(db_log): p>

  id -------- auto 
query ---- query 
create ---- curent time \  nuser ------ session-user 
  code>  pre> 
 
 

并将数据库上的每个插入,更新和删除操作存储到提及表中,例如 there table named( tbl_temp)有一些coumns,用户来自用户(user1) code>

 (DELETE * FROM tbl_temp,其中id = 1)  / pre> 
 
 

此处此查询的运行方式与(delete.php)的页面相同.. it应将查询保存在 db_log strong> p>的表中 n

  id = 1 
query = DELETE * FROM tbl_temp其中id = 1 
create = datetime 
user = user1 
  code>  pre> 
 
 

以便 我将如何能够记录用户在数据库上的每个动作并控制用户活动, p>

1 - 这里我需要将查询传递给 db_log strong>在每个页面中,或者我可以构建一个类 2-是否有任何好的解决方案或示例可供网络学习。 p> div>

You can create insert/update/delete triggers on all tables that you want to log. In that triggers you copy the data into your log table.

For instance:

delimiter |
CREATE TRIGGER log_insert_tbl_temp BEFORE INSERT ON tbl_temp
FOR EACH ROW BEGIN
   insert into db_log (table_name, action, id)
   select 'tbl_temp', 'insert', NEW.id;       
END
|
delimiter ;