Create Command Prompt to Continously Send MySQL Update Query - php

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.

Related

Code running every 24hours that updates data in database

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?

Replicate whole MySql Database

Am Using Master master MySql replication
its working fine. Both Server added
replicate-do-db=DB1
replicate-do-db=DB2
But I have n number of databases and going to increase day by day. It is much difficult to add databases in my.cnf every day. I used
replicate-ignore-db=information_schema in both server but that doesn't work.
Actually what I want how to replicate all database and upcoming databases automatically
if you have no filters in place it will replicate everything by default. run SHOW SLAVE STATUS on each side and look for the slave_IO_state, slave_IO_running,slave_SQL_running,last_xxx_error fields for clues as to what is happening. – IGGt
Slave_IO_Running Yes , Slave_SQL_Running No,Slave_IO_State Waiting for master to send event,Last_Error Error 'Can't drop database ,Last_Errno 1008
your suggestions helped me.Now ,Its working fine – Janmabhumi

duplicable database

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

PHP: how to update user logged in timer without using AJAX calls

I am creating a web application named Online Exam using PHP+MySQL and AngularJS. Now I am getting some trouble on project like changing the user looged in status. Let us take this condition as a example:
Suppose a authorized user/student successfully logged in online exam section(After successfully logged current time will be inserted in the db in exam_start_time column as unix timestamp format and exam_status will be set as 'ACTIVE`.
1hr(60 min) countdown timer is initialize for him/her as per the inserted exam_start_time in db.
Now suppose after 15 min the system shuts down automatically, then if user logged in again(In same system or other) then the countdown timer should be set for 45 minutes only.
Previously I was updating the last_activity_time in our table in every 10 sec(using ajax calls). but now I want to change this way, Is there any way like(socket or network programming using PHP) to update the column.
Here is my table structure which is managing user logged in status
Please give me some suggestions on it.
A Php socket server programming tutorial : http://www.christophh.net/2012/07/24/php-socket-programming/
Sockets, as Pascal Le Merrer mentioned, is IMO your best option. But beware of Apache! Every WebSocket holds one Apache thread, and Apache wasn't designed to do that. when too many (and by too many I mean few dozen) clients connect to your site, it will crash. (I've been there while trying to implement long polling/comet, ended up using NodeJS. If you're using nginx, it is more likely that it will become low on resources and effective, but there are also other ways. Take a look here:
Using WebSocket on Apache server
If you find this uncomfortable/hard to learn, try also another idea:
try to add hidden iFrame to your exam page, pointing to prepared site that updates database row. Use javascript to refresh this page every 10-15 seconds. Every refresh causes update of specific row in DB, using current date and time. It should work (not tested, but give it a try).

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.

Categories