Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL SQL parser (INSERT and UPDATE)

I am looking to parse INSERT and UPDATE MySQL SQL queries in PHP to determine what changes where made from what original data. Now this would be pretty easy to create, but I want to see if there are any existing libraries in PHP to do this.

Basically what I have is a table with all of the above queries that have been run on a database. I have already separated out the table name and type of query. I am looking to create a full change log for user viewing based on this data, so I need to get the values of the original INSERT and then changes that are made in each UPDATE. In the end I need field name and new value and with the record id(s). I'll do the rest of the checking/beautifying, including the column name to human readable and if a field value hasn't actually changed.

At the moment, I probably don't need to do multiple table UPDATE's, but it would be useful.

What libraries are there to do this?

like image 510
Darryl Hein Avatar asked Nov 12 '08 04:11

Darryl Hein


People also ask

What is $STMT in PHP MySQLi?

$stmt->bind_param("sss", $firstname, $lastname, $email); This function binds the parameters to the SQL query and tells the database what the parameters are. The "sss" argument lists the types of data that the parameters are. The s character tells mysql that the parameter is a string.


3 Answers

http://pear.php.net/package/SQL_Parser

http://sourceforge.net/projects/txtsql

http://code.google.com/p/php-sql-parser

For Perl there's more variety

like image 105
Vinko Vrsalovic Avatar answered Oct 24 '22 21:10

Vinko Vrsalovic


A little bit off the question, but maybe a suggestion worth thinking about:

Since MySQL 5.0, the support for triggers is quite good. If you want to keep a record of what changes have been made to a database, instead of storing the sql statements, you could also define insert/update triggers and define another table in which these values can be stored. You could, for example create a simple table having the fields

timestamp, user, field, old_value, new_value

and insert the respective values whenever a DML on one of your watched tables occured. To simplify this even more, you could add the field

table

to the "tracking table" to store all changes to all watched tables in one place.

See the MySQL manual for more infos about this topic.

like image 34
Dan Soap Avatar answered Oct 24 '22 19:10

Dan Soap


You may give it a chance: SQL Parse and Compile

like image 40
Tom Schaefer Avatar answered Oct 24 '22 21:10

Tom Schaefer