I want to make a log details for my database while a record insert into any table and update any table and delete any record from an table in MYSQL - PHP. Please give me some ideas.
-
[This](http://stackoverflow.com/a/12564435/2518525) might be able to help you. It's called audit tables – Darren May 20 '16 at 06:03
-
If you want to track each mysql operation you can create a txt file named with date and after executing each mysql statement you can update this file. – Afshan Shujat May 20 '16 at 06:06
-
Maybe it's not exactly what you are looking for, but http://dev.mysql.com/doc/refman/5.7/en/query-log.html – Isty001 May 20 '16 at 06:07
3 Answers
You've used laravel
tag, so I assume you want to find a 'Laravel way' to do it.
The best practice is to use Eloquent Events.
Each time when Laravel will hit the DB, it will try to run some of these events: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored
-ing
events are used before hitting DB, -ed
events are used right after hitting DB.
Example:
public function boot()
{
User::created(function () {
Log::info('User was just created successfully');
});
}

- 158,981
- 26
- 290
- 279
One solution is writing a helper function that logs successful insert/update sql queries if you have full control over your PHP application. A helper function can be called on every instance of insert/update queries which logs the features of the sql query you have. Some features for example can be the table name, the record id, and the type of operation i.e. insert or update, along with the system date.
In the module I have developed and have the link for below, these features are extracted from the original sql query automatically. This is one solution to the logging problem.
Here is how it works:
- We pass our original sql query to a function in the logging module;
- The function will extract sql features from the query;
- It will save all features to the log table we have setup in the database, along side system date and IP (Other features can be extracted also);
To see the logged data (as an interface to what we have logged):
- We call the main function in the front-end side of the module;
- This main function will get as parameter the number of most recent log records it should return;
- It will present the log records neatly, for example it will collect repeat number of operations on same record in to one entry. The CSS styling can be adjusted as needed.
- The whole interface can be minimized or restored using a show/hide button.

- 11
- 1
- 2