How to erase data from table automatically? - mysql

I have a table
it has four fields :Phone_number(primary key),City_name,Category_product and sub_Category
I am develoing a php application such based on an sms gateway.
whenver a user sends and sms requesting a product service,
his details will be saved in this table.
Now when a user sends an SMS ,i will store the details in the table,and
wait for the user to send back an sms with further details as per required.
Now what i want to do is:
if a user does not respond in lets say 15 min his details should be deleted from the table automatically.
How should i proceed?

You add a column to your table that stores the time it was sent.
Then you have to choices, you can either run cron jobs to clean out the data, or you clean up the records that are not valid when another user sends a sms, etc. Since you store the time with the data you can easily change the timeout from 15 minutes later on, too.

You have to use some scheduling. PHP dont have any native support for scheduling. What I would suggest is create a descktop applicaion or a service which listens the table for SMS response. If it did not get response then it will delete the correspond record. Your job will be running infinitely.

If this is going to be a table with many records, you should set up a pre-order table and then copy the record over when the order is made. In this way, you can clean up the pre-order table anytime you want if it starts getting slow.
As Ben said, it should have a timestamp for checks.

Related

MySQL Trigger After Any Changes

I am looking for a way to create a trigger after any changes occur in a table on any row or field.
I want for my web app to automatically refresh if they're have been any changes to the data since it was last loaded. For this I need a "modified_on" attribute for a table which will apply to the whole table, not just a row.
Not sure what database triggers have to do with this problem, as they are not going to be able to trigger any behavior at the web application level. You will need to build logic in your web application to inspect the data looking for a change. Most likely, this would take the form of some some-client triggered refresh process (i.e. AJAX), which would need to call a application script that would take information from the client on when it last checked for an update and compare it to the most recently updated row(s) in the table. As long as you have a timestamp/datetime field on the table and update each row when it is updated, you can retrieve all updated rows via a simple query such as
SELECT {fields} FROM {table}
WHERE {timestamp field} > '{last time checked}'
I you want, you could use this to only update those rows in the application view which need updating rather than re-rendering the whole table (this would minimize response bandwidth/download time, rendering time, etc.). If you simply want to check if the table has been updated from some certain, but don't care about individual rows, you can just check that the above query returns 1 or more rows.
If you don't want the client application view to have to check at regular intervals (as would likely be done with AJAX), you might also consider websockets or similar to enable bi-directional client-server communication, but this still wouldn't change the fact that your server-side application would need to query the database to look for changed records.

How to make an "out-of-date" notification for a MySQL database?

I'm something of a MySQL newbie, and I was wondering if there was any way to set up a notification system so that an e-mail notification would be sent any time an entry hasn't been updated for a specific amount of time (say, 6 months). Preferably, it would be done by specific category rather than row. Any tips?
Thanks!
you can add a field storing the timestamp of the last update, and run daily job (very very small script)

MySQL - Table format for Click stats

I'm currently developing an URL shortening service. I want to allow users to see the stats for their URLs. How has to be the table.
First, it has to be the url ID, but then, how I can sort the clicks per day?
It really depends on what stats you want to be able to display. In the absolute most general case, you could have two columns: URL ID, and a timestamp of when someone used that URL, insert one row every time someone uses a URL through your service. This will generate a lot of rows, but you'll be able to get any statistics that you want.
I doubt that you need to-the-second statistics forever though, so I'd suggest setting up a scheduled job to run once a day or so, and "roll up" the statistics for the day into a second table. The second table could have 3 columns: URL ID, date, and number of clicks. Every day, go through the first table that contains every click, figure out how many clicks there were for each URL, and insert a "summary" row into the second table. Then delete all the individual click-rows from the first table.
Make sense?

How to communicate between two instances of open MDB with same form?

In VBA (Access 2000) is there anyway to send information to a form between two open instances of the database?
For example :
User 1 has an instance open of DB.MDB on his PC and has FormOne open. User 2 has another instance of DB.MDB open on her PC and has FormOne open.
Can User 1 maniuplate the contents of a textbox on User 2's FormOne instance (ie. sending a message similar to a chat client)?
You could store data to a table and update the form or subform on a timer.
Like Remou, I think the table method is as you are going to get. You can optimize the querying by maintaining a one-record table that has the value of the last update. Then have your timer form check to see if the value has changed since the the timer last triggered, this will tell the timer to check the chat table.
In the alternative you can have records deleted as soon as they are read to keep the table small.
You will find that all the record creation/deletion will bloat your database though, so be sure to compact it regularly.
Lastly if all users have access to a shared drive you could just store the messages in a text file instead of a table.
Another issue is of course eavesdropping (with tables or files). You could minimize this by:
Obfuscating/encrypting the text before it is written and deobfuscating it when it is read. Deleting the record as soon as it is read by it's target.
Hiding the file/table. For files use: SetAttr myFile, vbSystem or vbHidden
For Tables prefix the table name with USys_ and make the table hidden.
All that said, it's still going to be a sorry substitute for a chat client. It will slow down the database and possibly slow down the shared drive. I would think long and hard about why I need this, and if it's really the best approach.

mysql delete row when user exits

i am in a situation where i want the db to delete a row when the user exits the application. even in the middle of the application. how can it be done?
to be more specific, consider a survey of 10 questions. i am storing the user's selections in a DB. now suppose the user exits at the 5th question. i want to delete the user's records then and there. so that if the user wishes to start again. s/he can start afresh.
I think you need to rethink your design. You should start a new session each time the user starts the survey. Clean up abandoned answers on a scheduled basis.
Never depend on browser close to perform actions like this.
You need a watchdog function that is called regularly. It compares it's list of logged in users with the real list. When there is a conflict it performs the SQL operation you require.