Code running every 24hours that updates data in database - php

I want to ask how can I do something like this.
I want every 24 hours to update the value in the table in the database
and I donĀ“t know how it can be done.
I need to it to be automatic, that means I won't be doing anything it will just do it on itself when its time
I am using MySQL database with
PHP version is 7.0
So how it can be done?
I don't need code (but I don't mind) but the mechanism, I want just description can I put some code into the database and it will do it automatically or run some file on server or?

Related

Create Command Prompt to Continously Send MySQL Update Query

I am hosting a game server on my LAN and I would like to show players if the server is online or not. Currently, the database it uses shows the time that the database turned on, but if the DOS windows that run the game are closed, the game is closed.. but the database doesn't reflect that. What I would like to do is add a field to the database for an entry that is updated every 15 minutes, showing if the server is on or not.
UPDATE `server` SET lastupdated='time()'
Clearly not identical to that.. but that's the idea I'm going after. Then my website will show as online as long as the lastupdated is no more than 15 minutes old. I just have absolutely no idea where to start or how to create this.
The game server is on my computer, but the database is on my web host. So I can't run a local mysql query either. Any help would be awesome.
If the column doesn't already exist, you're probably going to have to use an alter table to add a column to db_name.table_name.
Once the column is added, I think your DB would now be ready to accept queries (updates/selects) to display the last updated time.

Truncate database automatically without event scheduler?

My host (blueangelhost.com) claims that I can't use the event scheduler because it takes up too many resources. I have access to cron jobs in cPanel, but I've tried and they don't seem to work.
So, my question: Is there any kind of efficient PHP code that will automatically truncate a MySQL table in a database?
Well, if it needs to be automatic, or at a specific time, not really. But you could have your website trigger the script when someone gets on it, here's the approach you could use:
On a script that is run on every page (header, menu,footer, layout):
Check in DB or file, the date of the last truncate;
If the date is yesterday, run the truncate;
Change DB or file and put current date;
This way, it will execute once a day. But never at the same time, and not if no one walks on your website for a whole day.

PHP :What is the right approach for auto deleting a row on a database and saved data on server

Hello guys I need an advice with these situation :
For example I have a free classified posting website where in a user can post classified ads..
The ad would be listed on the website for a maximum of 30 days then on 31st day it will automatically be deleted on the database as well as the images on the server.. The question is :
$db_ad_tbl('id','user_id','title','description',timestamp);
What is the right approach for doing this?
Can anyone suggest tutorials/links that covers this the same situation?
Another approach that does not require cron is to use MySQL events. If you can come up with the correct query, you can set it as a recurring event. phpMyAdmin 4.0.x supports events handling from the interface.
See http://dev.mysql.com/doc/refman/5.5/en/events.html.
As Barmar has noted you should add a cronjob for this task. You can write a simple php script and then add it to your crontab with something like:
1 0 * * * php -f /path/to/file/clean.php
This means that the php file will be executed every day at midnight.
Just a few notes:
the file should not be in your web folder
you might want to do some tests and report errors by email(such as unable to connect to db)
If you build more of thees you should keep a list of them somewhere in case you switch servers(or the server dies)
if you use a config file(ex:to store your db connection details), you should make sure that it is accessible by the user that the cronjob works with.
Most hosting platforms allow for crontab editing and run them with the same user they run the web server so it should not be a problem.
There is really no other good solution to this then creating cron job. This is of course if you don't check the time stamp every time you get the data from the database.You can then delete it if it is bigger then the expiry data (DELETE FROM my_table WHERE timestamp>[Expiry Timestamp] ). This is of course risky, since you will have to include the timestamp every time you try a count, and risk storing everything forever if no expired resource is ever requested from the database.

Yahoo finance Historical Daily data to mysql daily

Another Yahoo Finance question.
I need a php function that will check if the DB is up-to-date (meaning there is no new info to download) & if not download the needed info ( I can already do this part).
What I have already:
I can download the info manually. When I say manually, I mean I can download it by going at the end of the day & calling my script "manually". So, I don't need help with that.
What I need help with is:
//Checking if the DB is up-to-date & if not, then update it
If it helps, I will call the function from a checkbox in an html form that will have the checkbox update DB. It's done this way, because it resides on a local box & will not be online all the time.
However, if it is easier to simply host it & do it via cron job, that is a consideration as well.
What determines if the db is up to date? If its the time of the last update, then you need to store the time of the last update somewhere, and then check it. If its the data itself, then you need to download the data to check.
setting up a cron job, or scheduled task, seems to be a separate concern.

Is there a way to see when a php script last ran?

I am looking to make a script that runs and uses the time stamp of the last time it ran as a parameter to retrieve results that have been updated since that time. We were thinking of creating a database table and having it update that and then retrieve the date from there, but I was looking for any other approach that someone might suggest.
Using a database table to store the last run time is probably the easiest approach, especially if you already have that infrastructure in place. A nice thing about this method is that you can write the run time right before the script terminates, in case it runs for a long time and you do not want it to start up again too soon.
Alternatively you could either write a timestamp to file (which has it's own set of issues) or attempt to fish it out of a log file (for example, the web access log if the script is being run that way) but both of those seem harder.
This might work: http://us.php.net/manual/en/function.fileatime.php (pass it $_SERVER['SCRIPT_FILENAME'])
Your best result would be to store your last run time. You could do this in a database if you need historical information, or you can just have a file that stores it.
Depending on how you run the script, you may be able to see it in your logs, but storing it yourself will be easier.

Categories