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
5
  • 4
    use cron job for run php file in specified time Commented Apr 24, 2013 at 16:17
  • thank you sir, but i am not able to use cron whole day i have searched for this but client has denied that i have to use only php, or else i can do with ajax or jquery if possible, anyway thank you for review my question. Commented Apr 24, 2013 at 16:22
  • 1
    Please only put the code in the code tag. Crontab is really the only way you're going to get it done. Commented Apr 24, 2013 at 16:24
  • Well sir, i have no much idea about cron or crontab, let me check it out first.. Commented Apr 24, 2013 at 16:26
  • Why are you checking for mysql_error() on the fopen()? Commented Apr 24, 2013 at 16:39

2 Answers 2

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.

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

Comments

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.

3 Comments

actually i need script which run frequently which should run not a single time in a one day but multiple times..
if SOME 1 CAN HELP ME WITH AJAX OR JQUERY THAT IS ALSO REALLY APPRECIATE, STILL STRUGGLING FOR LOOKING SUCH A CODE
@Prerak Dave It is bad etiquette to use all caps locks.

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.