I created a script that register for news letter. but before accepting this processes the registered mail must be validate. so ... after the registration , the scrip insert data into row for the news_letter thable with this value: reg_mail,val_code,reg_time and ...
Until now , this process is done after registration. The script creates a random code and get NOW() time. It then saves in database.
After that if someone else go to reg page, the script sends a query to database and deletes rows that passed for example 5 hours.
I want to change this process and tell mysql to automatically delete rows that pass specific time without send any query to database.
Is it possible?
If so, how can I do that?
It's not possible. The best you can do with pure mysql is periodically trigger a stored procedure to clean the database.
If you want this kind of feature, you'll have to use other kind of storage (memcached or cassandra for example manage ttl pretty well) or layer of application to keep in memory id to expires (a simple process reading a file in which you write a list of events to expire with a timestamp could be enough).
Related
I have a database that has 20 rows each row I had set a Boolean value to it, so it is by default zero and when a row gets viewed its value changes to 1
I want the database to send any kind of signal that when 10 rows their value change from zero to 1, a certain PHP file fires up and starts a process that will affect only these 10 rows
How can I do that?
Thanks in advance
I would say, query from the php file every set amount of time to your database
The other way, database to execute a php file is almost impossible.
If you are using mySQL as database, a trigger could invoke the sys_exec() UDF available here: https://github.com/mysqludf/lib_mysqludf_sys#readme
So, there might be a possibility, actually, via an UDF function that would launch the php executable/script; not that easy, but seems possible ;-)
Invoking php from mysql is impossible, all you can do is set cron jobs for it. Cron job check mysql after certain interval of time and run the respected code
Every database is only a storage and it is its purpose in the system. Don't try to trigger any external process by the storage. The communication with the storage should be only a one way.
Rather think how to trigger your process from outside. Generally, there are two approaches:
a script that will check your database data in some interval like 1s, 10s, 1min or whatever would fit for a particular process
the current process that is updating your data can check your data and trigger another process if needed.
You can not trigger external file/script from mysql.
What you can do is create a cron job which run after certain interval of time which check database and perform certain operations.
Is there a way that i can copy my database (a) from localhost and place it intro database (b) localhost?
I was thinking mb php has some manually (on refresh page opera refresh page every 1 seconds) or automatic php get from db(a) post to db (b) anything like this possible ?
What i need it for ...in to db (a) i store new members and every 30 minutes db (a) deletes itself, but db (b) its always full. So even if myweb.com gets hacked no one knows to hack myweb1.com if that makes any sense. One website hides the other
Its very hard to answer your question becouse we dont know much about your web and SQL servers.
If you have 2 SQL servers or 2 instances, or 2 separate databases you can easy create SQL JOB
and schedlue it to execute every 30 min or so.
Job would be simple to select data from DB(a) and insert them in DB(b) then delete records in DB(a) .. you need to make it in SQL transaction so new records added in DB(a) when you move records dont get deleteded
What would be the best way to achieve an undo function in a PHP CRUD application? The only solution I've been able to come up with is using a sort of buffer table in my database that is periodically wiped.
If the user clicks the "Undo" button after deleting a record for example, the id of the last change will be passed to a handler which will pull the buffer record and reinstate it into the main table for that data type. If the "Undo" is not done in say, 4 or 5 minutes, a reaper script will drop the entry.
Does this sound feasible? Is there a better way of accomplishing this?
You could use a flag field in your database to mark a row for delete.
And you can setup task (crontab in linux) to delete all rows with delete flag set to true and time difference > to 5 mins.
I've learned to not delete anything, but simply do as Ignacio Ocampo stated by using a flag column in your DB such as status. By default set the status column to open. If your client clicks your delete button, just update that records status column to void, or deleted..
In doing this, you'll need to update your data request to pull only those records with the status column set to open. This allows the data to not be lost, but also not seen.
all undo(s) or redo(s) if applicable can reset the open status to - or + 1 record sorted by a timestamp column.
If db space is at a premium, and you need to remove old data then crontab does work, but I prefer the simplicity phpmyadmin conjob to loop a file that will wipe all void or deleted records older than time()-'(last cron run).
Depending on what and how you're building, you might also want to consider using one of the following solutions.
1) A pure PHP CRUD solution would be something along the lines you've mentioned, with also possibly storing cookies on the client side to track which actions are being done. Every action a new cookie is created, then your application will only have to sort the cookies by date and time. You could also set the cookies to be automatically expire after x amount of time. (Although I would expire after a x amount of steps, instead of time)
2) If you are able to use HTML5 local storage (http://www.w3schools.com/html/html5_webstorage.asp) along with some Javascript would be perfect for this, since you wouldn't have to wait around for the server to respond everytime 'undo' is clicked since all the processing would be handled locally.
For a two-player game, I need to send updated data to player every 30 seconds.
I have a table (ideally 4 tables) from where I need to select data and sent to user once he/she login. Since it is multi-player interaction game, data needs to be sync every 30-60 seconds.
My problem is, I've a very heavy query to run every 30-60 seconds. So ideally, I should send only updated and new rows to the player during sync (Its also a front end requirement for IPhone/Android game, app don't want whole data during every sync operation).
I went through MySQL: difference of two result sets and hope I'll get only updated/new records through SQL but problem is, how do I save result of last query.
Even if I save first result in Session (probably not recommended) that record will be useless as soon as new row inserted or updated. Updating session record again will definitely put lot of pressure on the server.
Can someone please suggest the best way to achieve this requirement; Not detailed solution, just some hint/link will be sufficient.
Basically, this isn't that hard. Let me provide you with a step plan.
Add a datetime field to each table you want to do this on
In each of your updating queries, set this field to NOW()
Make sure that the application adds the time of its last update to all its requests
Have the server add the time of the update to result it send to the app (which also sends the updated rows)
Can't you just timestamp everything?
Give every row in the tables a timestamp called something like "last_updated"
In the query, filter out all entries with a last_updated that is before the last time the query was executed (or possibly the latest last_updated that the client got the last time it called the server)
I am not very experienced with advanced MySQL but I was wondering what is the best way to implement a time out. I have an iPhone app that 'locks' a row in a table while it is creating the content to be written back to it. It's just a BOOL column that I toggle back and forth. The problem is, if my app crashes or they lose network connection while it is locked, it will not unlock. I would like to set a 5 minute timer or something on the server to unlock the row if they fail to complete the process within that time. My web api is written in PHP.
Can I do this with MySQL? Do I need to create a cron job?
You could create a server-side application (cron job could work) that periodically looks at rows with a lock and unlocks them if they've expired. To know whether they have expired, you need to include a datetime column which is set to the current time when the lock is applied.
Alternatively, you can skip the server-side app and just let your iPhone app check the datetime, and reset the lock if it's been long enough.
In either case, the key is to add a datetime column and utilize that to know the age of the lock.