cron, php code

Discussion in 'Programming' started by starsi, Apr 24, 2009.

  1. #1
    Need help with cron - database coding.

    I'm trying to set up a cron on a game site of mine. The game updates the energy, health and income of the user.

    One of them is working, updating the user's health and energy, but somehow the income doesn't update.

    I used the advanced unix with the following cron code:

    env php -q /home/USERNAME/public_html/fightclub/cron/usercashcron.php


    Here's the usercashcron.php file

    <?php

    include "../includes/config.php";

    //====================================
    // Get User Income
    //====================================
    function get_income( $id ) {
    $res = query("SELECT * FROM `cu_assest_list` WHERE `aid`=$id");
    while( $row = mysql_fetch_array($res) ) {
    $income = $row[aincome];
    }
    return $income;
    }

    //====================================
    // Update User Cash
    //====================================
    function update_cash( $u ) {

    $total_cash = 0;

    $res = query("SELECT * FROM `cu_assest_log` WHERE `userid`=$u");
    while( $row = mysql_fetch_array($res) ) {
    $total_cash = $total_cash + ( $row[quantity] * get_income($row[aid]) );
    }

    $res = query("UPDATE `cu_users` SET `ucash`=(`ucash`+ $total_cash) WHERE `userid`=$u");

    }

    ?>
     
    starsi, Apr 24, 2009 IP
  2. joep1978

    joep1978 Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Make sure all your include files are specified with absolute paths as cron will not run the script in the directory it resides.
     
    joep1978, Apr 24, 2009 IP
  3. Nitroshock

    Nitroshock Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    joep's probably got it, looks like it's probably your include files. Cron needs the full path: /path/to/config.php
     
    Nitroshock, Apr 24, 2009 IP
  4. lephron

    lephron Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    instead of "../" do dirname(dirname(__FILE__)) . "/"
     
    lephron, Apr 25, 2009 IP
  5. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #5
    I had this problem

    execute it as cURL instead of PHP and it worked for me

    mine looks like:
    curl /home/directory/to/html/system/file.php
     
    tguillea, Apr 26, 2009 IP
  6. starsi

    starsi Peon

    Messages:
    84
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for all the replies. I had someone else do it. Just in case anyone's interested in the solution they came up with...

    The programmer created another file filet.php which has the following code:

    <?php
    include "includes/config.php";
    //====================================
    // Update User Cash
    //====================================


    $res3 = query("SELECT cu_assest_log.userid, cu_assest_log.aid, cu_assest_log.quantity, cu_assest_list.aincome FROM `cu_assest_log`
    LEFT JOIN cu_assest_list ON cu_assest_log.aid=cu_assest_list.aid
    LEFT JOIN cu_users ON cu_assest_log.userid=cu_users.userid");
    while( $row = mysql_fetch_array($res3) ) {
    $u=$row['userid'];
    $aid=$row['aid'];
    $qty=$row['quantity'];
    $income=$row['aincome'];


    $total_cash = 0;

    $total_cash = $total_cash + ( $qty * $income );




    $res = query("UPDATE `cu_users` SET `ucash`=(`ucash`+ $total_cash) WHERE `userid`=$u");
    }

    ?>


    For the cron job in advanced unix, it looks like this:

    ***** env php -q /home/USERNAME/public_html/SUBDIR/cron/usercashcron.php
    */5 **** env php -q /home/USERNAME/public_html/SUBDIR/cron/usercron.php
    0**** env php -q /home/USERNAME/public_html/SUBDIR/filet.php

    */5 apparently stands for 5 minutes, i.e., the game upgrades the health function every 5 minutes.
     
    starsi, Jun 2, 2009 IP