0

I am very new in php/mysql. After searching everywhere I am asking here: my client has provided me one txt file, now I need a script to run frequently so that txt new data file will run and also at the same time update a database.

One option I have read about is Cron, but my client denied that solution, so I can only go with a PHP (or, if possible, ajax or jquery) based solution.

My code so far:

<?php
$string = file_get_contents("mytest.php/test1.txt", "r");
$myFile = "E:/path/test.txt";
$fh = fopen($myFile, 'w') or die("could not open: .mysql_error());
fwrite($fh,$string);
fclose($fh);

// Here is the connection file
$sql=mysql_connect("localhost","root","");
if(!$sql) {
  die("couldnotconnect:", . mysql_error());
}
mysql_select_db("timer"); // databse name
$result=mysql_query("LOADDATAINFILE'$myFile'" ."INTOTABLEchangedtFieldsTERMINATEDBY''");
if (!$result) {
  die("couldnotload . " . mysql_error());
}
?>

But this code is not working properly. Basically just another text file through I just want to update my database like:

Database Field:

Name (varchar 250)
LastModification timestamp
Muc
  • 1,464
  • 2
  • 13
  • 31

2 Answers2

1

Your client needs to understand that cron is the correct solution to this problem. If your script needs to run on a regular schedule, cron is the way to accomplish it.

If you are unable to convince him, there are two other less optimal options. The first is to make your script run all the time without exiting. It will sleep for a period, wake up, check if the current time matches the scheduled run time, and act accordingly.

The second is to make use of an online scheduling service to execute your script via an HTTP request at the specified time. The online scheduling service will use cron under the hood, but your client doesn't need to know that.

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
0

You don't necessarily need the scheduled task/CRON to be run on the webserver. You can have the PHP file on your webserver, and have a scheduled task or cron script on another machine that called the PHP script (via HTTP GET or SSH terminal) and this would solve your problem.

You could also schedule this call on 2 different clients for redundancy and have a flag (for that day) to be set to true in order to gaurantee the PHP script is only run once daily.

When I mean flag I mean a database row, a value in a file or anything other way to check that the script has been run for that day.

Menelaos
  • 23,508
  • 18
  • 90
  • 155