I need a way to constantly (in a loop) check if a new MySQL row was added, and if so, do some thing with it, specifically send a notification to users that it pertains to, but I can handle that. I just need to know how to execute code when the number of MySQL rows changes.
You might wish to consider using a MySQL trigger on insert and/or delete:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
http://net.tutsplus.com/tutorials/databases/introduction-to-mysql-triggers/
Related
Basically, I am trying to create an interface that will tell an administrator "Hey, we ran this query, and we weren't so sure about it, so if it broke things click here to undo it".
The easiest way I can think to do this is to somehow figure out what tables and cells an identified "risky" query writes to, and store this data along with some bookkeeping data in a "backups" table, so that if necessary the fields can be repopulated with their original contents.
How do I go about figuring out which fields get overwritten by a particular (possibly complicated) mysql command?
Edit: "risky" in terms of completing successfully but doing unwanted things, not in terms of throwing an error or failing and leaving the system in an inconsistent state.
I suggest the following things:
- add an AFTER UPDATE trigger to every table you want to monitor
- create a copy of every table (example: [yourtable]_backup) you want to monitor
- in all AFTER UPDATE triggers, add code: INSERT INTO yourtable_backup VALUES(OLD.field1, OLD.field2..., OLD.fieldN)
How it works: the AFTER UPDATE trigger detects an update of the table, and backups the old values into the backup table
Important: you need to use INNODB table format for triggers to work. Triggers don't work with MyISAM tables.
You may add a timestamp field to the backup tables to know when each row was inserted.
Documentation: http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html
I need to have a button to fire an action to copy all records from a defined client from one database to another with php.
The template database has 12 tables (diferent rows on each) but all with the row client_id to make the WHERE clausule work properly.
The question is, how do I do this?
Thanks,
Pluda
Since PHP is a Server-side programming language, you can't copy something from the client. You can however upload Data (like XML), parse it and then insert it into your MySQL Database.
If you want to copy records from one to another database, you might want to read from the Database and save them in a format like SQL. Then, you could send those querys to the second Database.
An advise at this point: If you need to make the same Query (with different values) over and over again, you should use a PreparedStatement. It will be compiled in the Database and then just filled out with new values. This is way faster then using an Insert every time.
Is it possible to queue client requests for accessing database in MySQL. I am trying to do this for concurrency management. MySQL Locks can be used but somehow I am not able to get the desired outcome.
Effectively what I am trying to do is:
INSERT something in a new row
SELECT a column from that row
Store that value in a variable
The issue comes up when two different clients INSERT at the same time, thus variables for both clients store the value of the last INSERT.
I worked the following alternative, but it failed in a few test runs, and the bug is quite evident:
INSERT
LOCK Table
SELECT
Store
UNLOCK
Thanks!
My best guess is that you have an auto-increment column and want to get its value after inserting a row. One option is to use LAST_INSERT_ID() (details here and here).
If this is not applicable, then please post some more details. What exactly are you trying to do and what queries are being fired?
I have a mysql database. What I'd like to do is perform an arbitrary action on it, and then figure out what changed. Something like this:
//assume connection to db already established
before();//saves db state
perform_action();//does stuff to db
diff();//prints what happened
I'd want it to output something like:
Row added in table_0 ]details]
Row added in table_1 [details]
Row modified in table_5 [details]
Row deleted in table_2 [details]
Any ideas?
To further clarify: You know how on stackoverflow, if you check a post's edits, you can see red lines/green highlights indicating what's been changed? I want something like that, but for mysql databases.
Instead of copying your whole database in order to save the state for a later diff, you might be better off by using triggers:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
When you setup appropriate triggers, you can log changes to a table - for example, you can setup a trigger that automatically logs the old values and the new values for every update. To see the changes, query the table that was filled by the trigger.
Of course, the trigger is not restricted to changes made by your application, it will also log updates done by other applications. But this is also the case if you diff the old version of the database with the new version of the database.
I think normally your application would log any interesting changes as it makes them. Or you would set up history tables for everything with datetimes.
To do it the way you describe, you could dump the contents of the database into a file before and after your action and do a diff on the two files. In php, you can check out xdiff: http://us.php.net/manual/en/book.xdiff.php
If this is something you're doing only occasionally in controlled circumstances to test some queries you're not sure about, you can dump and diff on the command line.
One way is to parse the log files, which will give you exact SQL statements executed in your database. I'm not exactly sure how to separate SQL statements made by your application from other applications (if thats the case)
The only thing I can think of is to do some combination of a few somewhat hackey things:
Save a [temporary?] table of row IDs, to check for new rows. If you need to know what was in deleted or modified rows before, you'll need to copy the whole DB, which would be rather messy.
Have each row have a datestamp that gets modified on update; grab rows for whom the updated datestamp is newer than when the analysis started.
Have a layer between your application and the database (if you have something like the classic $db->query(), it would make this easy), log queries sent, which can then be looked at.
I suppose the real question is if you want to know what queries are being executed against the DB, or if you want to know what they queries you're running are actually doing.
I am inserting a value in to the table using php-mysql its getting inserted correctly.The insertion file in php is called through the ajax request. My problem is in some of the firefox version the request is triggered twice at some times and the records are inserted 6times with same datas in the table..How to prevent such situation without using unique concept...
It sounds like the SQL does what it is told to. It's up to you to not execute the query with the same data over and over.
As you stated "in some of the firefox version the request is triggered twice", the problem is most likely in your client code, post that code for us to be able to help you.
(This should have been a comment but I'm too low on rep pts.)
MySQL offers a REPLACE command which is similar to INSERT but silently ignores duplicates. Documentation here.
Using jQuery might help you (if you're not already). You can ensure that your code isn't executed too early with jquery's built in $(document).ready() event.
Also look at what is triggering the ajax call, if it is being done more times than you expected. You could also do some server-side checking to make sure that a query was not already submitted.
Because you don't want to go with a UNIQUE index...
...I realized similiar problems by simply querying the table before the insert to look if the record already exists. I also implemented a time limit for the look up.
So this is what I do:
I check the data if it's valid for insert in the table.
I use this exact data for a select on the table, limited to all entries in the last 5 minutes.
If the select returns no result, I insert the data in the table, otherwise I ignore it as "dublicate entry".