【问题标题】:php and mysqli actions in cron jobs?cron 作业中的 php 和 mysqli 操作?
【发布时间】:2017-10-31 09:03:43
【问题描述】:

我通常“手动”更新我的网站,输入一个名为“enterheretoupdate.php”的页面。此页面每分钟刷新一次以完成我需要的所有工作,因此当此页面“打开”时,我的网站每分钟都在刷新。

“enterheretoupdate.php”有什么作用?它使与 mysql 相关的事情:创建表、从表中选择、向表中添加行等。除此之外,它还对 php 进行计算并更新 .json 文件。

我想创建一个 cron 作业,这样我就不必每分钟访问我的计算机上的“enterheretoupdate.php”来更新我的网站。

我对此很陌生,但我已经学会了如何创建一个 cron 作业(我使用 1and1)。我创建的示例 cron 作业,包括每分钟发送一封电子邮件,工作正常。

然后,我尝试将“enterheretoupdate.php”保存为 cron 作业,但它不起作用。 cron 作业可以做的事情是否有“限制”?我应该如何“翻译”我的 php 文件以使其作为 cron 作业工作?

非常欢迎任何帮助。

这就是我的 .php 文件的样子:

<?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++;
  }
}

你认为它适合 cron 工作吗?我应该如何进行?非常感谢!

【问题讨论】:

  • 通过 CLI/SSH 测试它,通过 cron 调用它应该没有问题。如果没有具体的错误消息,“不起作用”是无法回答的。
  • 检查您的 cron 文件路径,在您的 cron 文件中写入日志以检查其是否执行
  • Mario,这个 php 做的第一件事就是在 include("newcon.php") 处连接数据库。错误出现了,与数据库的连接,
  • Tejas Khutale,文件路径正确,正在执行。该错误与与数据库的连接有关,这是它所做的第一件事。
  • 您的 CRON 生产线是什么样的?我认为它应该类似于* * * * * php -f /path/to/my/file.php

标签: php cron


【解决方案1】:

我有类似的问题,但以下对我有用。

查看链接更改默认mysql权限 How to allow remote connection to mysql

现在更改 sql 连接文件中的 db_server 值 localhost127.0.0.1

在您的情况下,您似乎需要编辑文件../newcon.php

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 2023-03-22
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2015-10-29
    • 2010-09-12
    • 2012-11-12
    相关资源
    最近更新 更多