user2625007
user2625007

Reputation: 59

Posting data from database at regular time intervals automatically in php

I have questions stored in my database. I want to regularly post one question on my website from the database at a 24 hr interval automatically. Is there a way I can do that ?

Upvotes: 2

Views: 2071

Answers (9)

Rajesh Paul
Rajesh Paul

Reputation: 7019

Steps

  1. Create a PHP script to select and post a particular question randomly.

  2. In your main php script write an AJAX method(which will load the PHP script) which can be called using setInterval() using the following syntax-

    setInterval("AJAX_fun()", 24*3600*1000);

This statement will call the AJAX function in a periodic interval of 24hrs. For that you must know AJAX. I mean what should be the body of the AJAX to load the PHP script that you must have an idea of.

Another alternative

You can simply reload the page using javascript setInterval() function

i.e. <script>setInterval("window.location.reload()", 24*3600*1000);</script> and before that you have to select a question from the database randomly using a PHP logic.

Upvotes: 0

Alma Do
Alma Do

Reputation: 37365

You can do this with steps:

  1. Create normal PHP-script which will post your questions.
  2. Schedule your script with standard OS scheduler. It is cron for *nix (Win-versions exist too) or AT for Windows. To define certain interval - you should read scheduler's manual (for cron format is provided here)

Example (cron)

0 2 * * * /usr/bin/php /path/to/insert/script.php

-in this case every day at 02:00 AM cron will try to execute command /usr/bin/php /path/to/insert/script.php - i.e. if your script.php will extract your question from DB and post it - that will do the stuff.

Upvotes: 1

rohitr
rohitr

Reputation: 371

Create a php file put the code to fetch question form your database then set cronjob to excecute the file on perticular time or also you can execute file by including it your login or any other page which lods first by including that php file file so that when first user logs in it will execute.

Upvotes: 0

alandarev
alandarev

Reputation: 8635

Yes, you can. I will shortly outline the two most common solutions. The difficulty rises that PHP is not an always running program, but is a language executed on request and then shutdown on completion.

  1. Have some sort of init.php file on your webserver which is being included on every page. That script will check whether the time has passed since last question, and push a new question.
  2. On the other hand, you can add a cronjob which will execute your php script pushing the question. This solution is more robust, but requires access to a webserver you might not have.

Upvotes: 0

user2801966
user2801966

Reputation: 468

Write a script that will post one question on your website everyday and set a cron job to run that script once a day and you are done.

How to set a cron job , ask you hosting service provider , most of the hosts have this feature in cpanel

Upvotes: 0

ahmed
ahmed

Reputation: 360

The only way to do it correctly is to use cron jobs. You should take a look at the administration panel of your hosting service.

Upvotes: 0

AlexP
AlexP

Reputation: 9857

You should look into MySQL date functions.

A contrived example would be using CURDATE():

SELECT * FROM questions WHERE publish_date = CURDATE()

Storing the publish_date will mean you can dynamically load the question when that date arrives.

Upvotes: 0

AdamskiFTW
AdamskiFTW

Reputation: 109

Providing you could create a PHP script to select a different question each time, all you'd need to do would be to set up a cron to run the PHP script every 24 hours. You can find more info on cron here.

Upvotes: 0

Roopendra
Roopendra

Reputation: 7776

Yes you can do it by using Cron job . Set time interval and your file script location. It will automatically hit your script on that time interval.

Here is good tutorial : http://docs.phplist.com/SetupCronJob.html

Upvotes: 0

Related Questions