0

How to insert values into database table every two minutes?

I have code that doing every second that will explode my mysql database, which I do not want it happen. So I want to set it to 2 min.

$query="INSERT INTO $tablename VALUES ($temp,now())";

Here is the code for every second sent it to my databse.

and the code below is to set it two min , but is doesn't work

$timestamp=DATE_SUB(now(), INTERVAL 2 MINUTE);
$query="INSERT INTO $tablename VALUES ($temp,$timestamp)";

Some help me . thanks

16
  • 1
    You're only changing the value being stored, not the schedule itself. Where's the code that schedules this query's execution? Commented Nov 8, 2013 at 19:26
  • Maybe you can explain to us why you need to insert data every 2 minutes ? Commented Nov 8, 2013 at 19:27
  • I want temperature data send it to my database every 2 minutes,not every second Commented Nov 8, 2013 at 19:41
  • @Yang that actually seems to be like valid reason to put data in DB every two minutes. You need to set up some kind of task scheduling application that will run your script every two minutes. Php scripts generally run in an instant, and once they finish, they're finished, there's no way to keep them running forever. Task scheduling application under windows is task scheduler, under linux it's called Cron, look it up, you will find tutorials. Commented Nov 8, 2013 at 19:46
  • 1
    So I'm asking again: How is this code triggered? What's the procedure that actually calls the above? Is it a cron job? You do it manually from the command line? Do you have to constantly press [F5] on a browser window every seond? Is there a remote daemom that wgets it? Commented Nov 8, 2013 at 19:50

2 Answers 2

2

Maybe my answer will not satisfy you but it's a mistake to make a task every 2 minute with a web framework. When you close your web browser, the task will not continue. This kind of daemon task must be done with CRONTAB.

Sign up to request clarification or add additional context in comments.

15 Comments

This is not an answer. It belongs to a comment. So I think it satisfies pretty much noone.
@geomagas In fairness, it is the correct answer. OP is completely off course and his comment about wanting it to run every 2 minutes points towards the need for crontab or Windows task scheduler. Saying that it satisfied nobody is wrong imo.
Maybe, I admit I hesitated to write as a comment
I edited my answer. I deleted my question in my answer and I writed it in comments. Sorry
@geomagas I agree. However, I'm stick of people on Stackoverflow giving the technically correct answer instead of the answer that will actually push the person in the right direction. OP wants to execute the code in question every two minutes. Crontab is the perfect tool for that.
|
0

Apart from completely wrong approach (you need cron daemon to control tasks running every n minutes) , possibly what you're trying to do is to use mysql's date_sub()

$mysqlString= 'DATE_SUB(now(), INTERVAL 2 MINUTE)'; //this makes it a string
$query="INSERT INTO $tablename VALUES ($temp, {$mysqlString})";
echo $query;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.