Hello.

I am currently making a php game for a bit of fun but I have got stuck at one point. I am trying to create a cron job that runs a function which updates the database. Below I have posted bits of the code.

Function

function aircraftdelivery($day, $month, $year, $delivery_day, $delivery_month, $delivery_year) {

if ($day == "$delivery_day" && $month == "$delivery_month" && $year == "$delivery_year") {

return ($delivered = ("UPDATE Aircraft SET Delivered = 'Yes' WHERE Delivery_Day = '" . $day . "'"));

} 

}

Cron Job

<?

include("include/db.php");
include("include/config.php");
include("include/functions.php");

?>

<?

$query = ("SELECT * FROM Date");
$row = (mysql_fetch_array(mysql_query($query)));

$day = $row['Day'];
$month = $row['Month'];
$year = $row['Year'];

$query2 = ("SELECT * FROM Aircraft");
$row2 = (mysql_fetch_array(mysql_query($query2)));

$delivery_day = $row2['Delivery_Day'];
$delivery_month = $row2['Delivery_Month'];
$delivery_year = $row2['Delivery_Year'];

?>

<?

$date = gamedate($day, $month, $year);

$delivered = aircraftdelivery($day, $month, $year, $delivery_day, $delivery_month, $delivery_year);

?>

<?

mysql_query($date);

mysql_query($delivered);

?>

The $date mysql query updates the date in the database every minute and its stored by day, month and year. The $delivered query is trying to update a field in another table if the date of the game is the same as that in the table where the other field is.

It is not working for some reason.

It works if you enter the date numbers into the variables rather than getting them from the database. I am wondering if its missing the date. But not sure.

Any help would be much appreciated.

Ryan

    Before addressing a few issues I noticed in your code, I'd like to first ask this: Why the heck are you doing this in the first place?!

    You're running this every minute and generating a lot of queries for what seems like nothing. The only purpose, as far as I can tell, is to update a boolean type of variable to indicate that a certain date has passed. Is that correct?

    If so, why have that pseudo-boolean column in the DB in the first place? If you want to know whether the date has passed, just check if the date is <= to today's date.

      Its a game that I am making. The date is not the real date, its the game date which will run every 25/30 minutes (eg. 1 day = 25 minutes) rather than every minute. Its just to test it. Hope that explains the time.

      It would be brilliant if you could tell me the problems 🙂

      Thanks,

      Ryan.

        Write a Reply...