How can I compare data before and after update PHP and MySQL?
If I add some data in the database that can be updated, is there a way of comparing the newly updated data with the previous data?
Yes, that is what TRIGGERS are for.
Using the CREATE TRIGGER syntax you can create a trigger before an UPDATE or INSERT
CREATE TRIGGER insert_trigger_name BEFORE INSERT ON table
or
CREATE TRIGGER update_trigger_name BEFORE UPDATE ON table
Related
I was wondering if anyone could help answer this question. I currently have an SQL Query that's being displayed into a table using PHP. I use a separate PHP file to upload data to mySQL table and when I update my table with a new column I have to rerun the SQL Query to get it to update into the PHP table. I was wondering if there was a way using PHP to automatically run that SQL Query when there has been an update to the table.
You can use AJAX and it to run in every second or you can use
meta tag : refresh
this will refresh page with php table
I'm looking to pull a table's create syntax via mysql and php. Is it possible?
I need it for a file that creates table_x automatically every 10 days. Since I update the site constantly and create new fields I'd like the file to be dynamic and use the previous table (instead of me updating it manually each time).
Yes you can - use
SHOW CREATE TABLE tablename;
Actually if you want to 'reset' your database I would use TRUNCATE instead
TRUNCATE TABLE tablename
SHOW CREATE TABLE table_x;
...will output one record with the CREATE syntax. Is that what you're looking for?
i'm new to php . i'm just wondering which PHP script can detects if a MySQL database's table contains data . If yes , it will update the data with the submitted form data. If not(blank) it will insert the form data into it.
I know how to insert data using php just wondering how can i CHECK if data exist in a table.
Thanks and have a nice day .
You are looking for INSERT ... ON DUPLICATE KEY UPDATE.
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Just append the ON DUPLICATE KEY UPDATE x=y, z=a to your current INSERT and if the unique value you try to insert already exists, the row containing it is updated.
MySQL can help with that depending on your way to check (primary key ?) - see http://www.xaprb.com/blog/2006/02/21/flexible-insert-and-update-in-mysql/
Checking if data exists in a database is a read operation, so you'd use a SELECT statement. If the SELECT statement returns a non-empty recordset, then you know the data exists. Bonus: you also know the id number of the data you're looking for, for use in your update operation.
The INSERT... ON DUPLICATE KEY UPDATE is great if you know the unique id number of the row you're updating... which you might now.
The only issue with using a SELECT statement is that the operation is not guaranteed to be atomic - that is, this might happen:
- you run the SQL statement and get an empty set
- a different process inserts that form data into the database
- the first process, that ran the select statement, inserts the same form data
Good luck!
I have a mysql database with 12,000 entries, what i want setup is the ability to monitor a column in the database and if/when the column is altered for any entry it sends me an email with the details.
EDIT: I have access to mysql db, but not the script which works with it. So it should monitor it for changes...
You could create some triggers on the table, if your version of MySQL has them. A trigger can then invoke any function you care to create. A trigger has the advantage that any insertion or deletion or any update of the column will cause it to fire; you wouldn't have to change any other code to make it happen. See here for more... http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Create a trigger on update
Create another table (lets call it cron_table), where the trigger will insert information of the updated row (may be old value, new value etc)
Setup a cron, which will call a script which will check the cron_table and send email if any entry is found. Cron interval can be setup according to need.
--- If you could send email from trigger, there would be no need for a separate table and cron ---
try something similar to this , you can edit the function to send you and email if the query has insert and TABLE_NAME or COLUMN_NAME in it
set up one column to be a datetimestamp.
This will update on every change of the row. There you can run a sql query either via a cron job or after every few php queries to return you the list of changed rows since the last check.
Select * from tbl_myentries where EntryUpdated > '$TimeSinceLastCheck'
you need to understand Data Manipulation Language (DML) triggers
in my sql: use
CREATE TRIGGER salary_trigger
BEFORE UPDATE ON table_name
REFERENCING NEW ROW AS n, OLD ROW AS o
FOR EACH ROW
IF n.columnName <> o.columnname THEN
END IF;
;
Create a trigger on a change on your column, then insert it to another table as log table.
Run cron job on your table that will send you an email.
I am trying to find out when something was added to a database I maintain but the script that adds the date was working.
Is there a way to retrieve the date of the original INSERT command?
Something like this?
http://dev.mysql.com/doc/refman/5.0/en/query-log.html
make a new table with the key of the table to watch create a after insert trigger that inserts a new line into the watchout table with the id and the time inserted