简体   繁体   English

Cron作业的脚本不起作用。 PHP / MySQL

[英]Script for cron job not working. PHP/MySQL

I set up a cron job to run the following script every 30 min, Basically this script selects article info from stories table where field showing=0 And it than counts votes related to that article, the votes are located in votes table. 我设置了一个cron作业,每30分钟运行一次以下脚本,基本上,该脚本从“ stories”表中选择文章信息,其中“ field” 显示为“ 0”,并且比起与该文章相关的票数,该票数位于“ votes”表中。 itemname_field in votes table is equal to id field in stories table. 投票表中的itemname_field等于故事表中的id字段。 If you need more information on database structure and voting system, I describe it in depth here: 如果您需要有关数据库结构和投票系统的更多信息,请在此处进行详细介绍:
Set a cron job to update article information depending on its vote values 设置cron作业以根据其投票值更新文章信息

<?php 
    mysql_connect("localhost","username","password") or die (mysql_error("There was an error in connection"));
    mysql_select_db("database") or die (mysql_error("There was an error in connection"));

    $query = "select sum(vt.vote_value) as vote_value, vt.item_name from Votes vt join stories st on st.id = vt.item_name and st.showing = 0 group by vt.item_name";
    $result = mysql_query($query); 
    while($row = mysql_fetch_array($result))
    {
        if($row['vote_value'] > 9) {
            $showing = 1;
        }
        else if($row['vote_value'] < -9) {
            $showing = 2;
        }
        else {
            $showing = 0;
        }
        $query2 = "UPDATE `stories` SET `showing` = $showing WHERE `id` = '".$row['item_name']."'";
        mysql_query($query2); 
    }
?>

Can anyone suggest the reason, why this script is not working? 任何人都可以提出原因,为什么此脚本不起作用? If everything is alright with script, could it be something in cron job that needs to be done? 如果脚本一切正常,难道是cron工作中需要做的事情吗?

EDIT: when I run script in browser it gives the following error: 编辑:当我在浏览器中运行脚本时,出现以下错误:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

Which is this line while($row = mysql_fetch_array($result)) 这是哪一行while($row = mysql_fetch_array($result))

The issue was in this part: 问题出在这部分:

$query = "... from Votes vt join ...";

it should not be capital "V". 它不应为大写的“ V”。

The error message you quote means you have an error in your query. 您引用的错误消息表示查询中存在错误。 Run it though a mysql client to get more info, or echo mysql_error() in php. 通过mysql客户端运行它以获取更多信息,或在php中回显mysql_error()

In such cases mysql_query() returns boolean false, you can use this for error handling. 在这种情况下, mysql_query()返回boolean false,可以将其用于错误处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM