0

I usually update my site "by hand", entering in one page called "enterheretoupdate.php". This page refreshes every minute to do all the job I need, so while this page is "open", my site keeps refreshing every minute.

What does "enterheretoupdate.php" do? It makes things related to mysql: create tables, selects from tables, add rows to tables, etc. Apart from that, it also make calculations on php and updates .json files.

I would like to create a cron job, so that it is not necessary for me to visit "enterheretoupdate.php" on my computer for updating my site every minute.

I am quite new on this, but I have learned how to create a cron job (I use 1and1). The example cron job I have created, consisting on sending an email every minute, works fine.

But then, I tried to save "enterheretoupdate.php" as a cron job and it does not work. Is there a "limitation" on the things a cron job can do? How should I "translate" my php file to make it work as a cron job?

Any help is really welcome.

This is how my .php file looks like:

<?php
$page = $_SERVER['PHP_SELF'];
$sec = "10";

//Change 1 to reload, 0 to not to reload;
$reload=1;
$gamecode=7;
$cmp="EL";
$year=2017;
if ($reload==1) echo"<head><meta http-equiv='refresh'content=".$sec.";URL='".$page."?gamecode=".$gamecode."&cmp=".$cmp."&year=".$year."'></head>";


include("../newcon.php");
include("../formulas.php");
include_once("funLightCreateTables.php");
include_once("funLightFirstFive.php");
include_once("funLightChanges.php");
include_once("funLightLiveJsons.php");




if ($cmp=="EC") {$l="U";}
if ($cmp=="EL") {$l="E";}

//Check
$q="SELECT * FROM LightLiveSchedule WHERE year=".$year." and cmp=".$cmp." and gamecode=".$gamecode."";
$res=mysqli_query($link,$q);
while ($r=mysqli_fetch_assoc($res)){
  $started=$r['started'];
}

if ($started==0){
    LightCreateTables($cmp,$year,$gamecode);
    $q="UPDATE LightLiveSchedule SET started=1 WHERE year=".$year." and cmp=".$cmp." and gamecode=".$gamecode."";
    mysqli_query($link,$q);
}


//Read
$pbp=file_get_contents("http://thesite.com/data.json?gamecode=".$gamecode."&seasoncode=".$l.$year."");
$pbp = json_decode($pbp,true);

//Insert
mysqli_query($link,"Truncate P_Live_Temp_".$cmp."_".$year."_".$gamecode."");

$lres=0;
$vres=0;
$n=0;
for ($i=0;$i<=4;$i++){
  $nplays[$i]=count($pbp[$qtitle[$i]]);
  $ii=0;
  for ($j=0;$j<=$nplays[$i];$j++){
    //change results
    if ($pbp[$qtitle[$i]][$ii]['PUNTOS_A']!=null) {
        $lres=$pbp[$qtitle[$i]][$ii]['PUNTOS_A'];
    }
    if ($pbp[$qtitle[$i]][$ii]['PUNTOS_B']!=null) {
        $vres=$pbp[$qtitle[$i]][$ii]['PUNTOS_B'];
    }
    //clean
    if (strpos($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],"(")==0) {$play=$pbp[$qtitle[$i]][$ii]['CSDESCWEB'];}
    if (strpos($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],"(")>0) {$play=substr($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],0,strpos($pbp[$qtitle[$i]][$ii]['CSDESCWEB'],"(")-1);}
    //count
    $points=0;
    if ($play=="Three Pointer") {$points=3;}
    if ($play=="Two Pointer" or $play=="Lay Up" or $play=="Dunk") {$points=2;}
    if ($play=="Free Throw In") {$points=1;}
    //ntconsole=00:00 at End Game
    if ($play=="End Game") {$pbp[$qtitle[$i]][$ii]['NTCONSOLA']="00:00";}
    //insert
    $q="INSERT INTO P_Live_temp_".$cmp."_".$year."_".$gamecode." 
        (orden,shteam,shloc,shvis,quarter,minute,ntconsole,pcode,play,locres,visres,points) 
        VALUES
        (".$n.",'".$pbp[$qtitle[$i]][$ii]['NTEQUIPO']."','".$pbp['ca']."','".$pbp['cb']."',".($i+1).",
        ".$pbp[$qtitle[$i]][$ii]['MINUTO'].",'".$pbp[$qtitle[$i]][$ii]['NTCONSOLA']."',
        '".str_replace(" ","",substr($pbp[$qtitle[$i]][$ii]['NTJUGD'],1,10))."','".$play."',".$lres.",".$vres.",".$points.")";
    mysqli_query($link,$q);
    $ii++;
    $n++;
  }
}

Do you think it is suitable for a cron job? How should I proceed? Thanks a lot!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Javi
  • 170
  • 3
  • 13

1 Answers1

0

I had similar issues but the following worked for me.

See the link to change default mysql permission How to allow remote connection to mysql

Now change your db_server value in the sql connection file from localhost to 127.0.0.1

In your case the you need to edit the file ../newcon.php it seems.