...

View Full Version : Resolved MYSQL/PHP Update Problem



ryanhami
07-06-2011, 03:04 PM
Hello,

I am currently making a script for a bit of fun and have got stuck at a certain point. My script is a date thing that works with php and mysql. It runs by a cron job that I have setup. Below you can see the two different files. In the table in the mysql database, there are just 3 fields. Day, Month and Year.

Cron Job File


<?

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'];

?>

<?

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

?>

<?

mysql_query($date);

?>

Function File


<? function gamedate($day, $month, $year) {

if ($day == "31") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = Month + 1"));

} elseif ($month == "12" && $day == "31") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = 1 AND SET Year = Year + 1"));

} elseif ($month == "2" && $day == "28") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = Month + 1"));

} elseif ($month == "4" && $day == "30") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = Month + 1"));

} elseif ($month == "6" && $day == "30") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = Month + 1"));

} elseif ($month == "9" && $day == "30") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = Month + 1"));

} elseif ($month == "11" && $day == "30") {

return ($date = ("UPDATE Date SET Day = 1 AND SET Month = Month + 1"));

} else {

return ($date = ("UPDATE Date SET Day = Day + 1"));

}

}

?>

I am currently running the cron every minute to test it but it gets stuck at 31/1/2000

It should change to 1/2/2000 but it does not seem to want to do it and I cannot see anything wrong with it.

Any help would be much appreciated.

Thanks,

Ryan

Fumigator
07-06-2011, 04:23 PM
If you improve your code to check to make sure your query works, you'll see the error.



$result = mysql_query($date);
if (!$result)
{
die("Query Error! query: $date<br>Error:".mysql_error());
}


Your UPDATE query is bad syntax; it should be UPDATE table SET column1 = value, column2 = value WHERE column3 = value

ryanhami
07-06-2011, 04:31 PM
Thanks for the response. Where will that error be shown if its running as a cron job?

Fumigator
07-06-2011, 04:37 PM
Thanks for the response. Where will that error be shown if its running as a cron job?

Good point, you'll miss it with just an echo. Write it to a log file instead of echoing it.

But really, while writing your script, why are you messing with CRON too? Get the thing working outside of CRON first, then schedule it. But I believe fully in good logging so I would log errors 100% of the time.

ryanhami
07-06-2011, 04:47 PM
Got it working now, thanks for the help Fumigator :)

abduraooft
07-06-2011, 04:54 PM
UPDATE Date SET Day = '1', SET Month = Month + 1
Check the manual (http://dev.mysql.com/doc/refman/5.0/en/update.html) for the right syntax!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum