Jump to content

Cron jobs...


matty

Recommended Posts

Hey,

Ive been having some trouble with a cron job ive been trying to get working.

 

This is what i have so.

 

<?php

$db = mysql_connect("localhost", "USER", "PASS") or  Die("I cannot connect to the mysql server because: " . mysql_error());
mysql_select_db("DB") or Die("DB Error !");


$energy = mysql_query("SELECT * FROM  
userdb");
$energyall = mysql_fetch_array($energy);

if($energyall[energy] >= 100){
$mysql_query2 = Mysql_query("Update userdb set energy=100 where dead='N'");}
else{
$mysql_query2 = Mysql_query("Update userdb set energy=energy+5 where dead='N'");
}

 

 

Ive tried coding it different ways. Ive tried putting the 100 into a varible and using the veriable instead.

 

What is meant to happen is it is supposed to get all the users that arnt dead and then determine weather they have 100 energy (which is max) and therefore they cannot get more or if they have less than 100 its gives them +5 to there current energy.

I have gotten it so it will do this but it will not do it again. =S

All it does is when i use up some energy for i say have only 50 left once the cron has ran it will set it too 100 instead of adding 5.

 

Any help would be appicated, im not that familiar with crons.

Matt,

Link to comment
https://forums.phpfreaks.com/topic/39551-cron-jobs/
Share on other sites

ok then add a while loop that works on each row returned

<?php

$db = mysql_connect("localhost", "USER", "PASS") or  Die("I cannot connect to the mysql server because: " . mysql_error());
mysql_select_db("DB") or Die("DB Error !");


$energy = mysql_query("SELECT * FROM  
userdb");
while ($energyall = mysql_fetch_array($energy))
{
     $energyid = $energyall['idvalue']; //needs the correct id value calling here
     if($energyall['energy'] >= 100)
     {
         $mysql_query2 = mysql_query("Update userdb set energy=100 where dead='N' WHERE idvalue = '$energyid'");
     }
     else
     {
         $mysql_query2 = mysql_query("Update userdb set energy=energy+5 where dead='N' WHERE idvalue = '$energyid'");
     }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/39551-cron-jobs/#findComment-191194
Share on other sites

My advice would be to:

 

  • find php by typing "whereis php" on the command prompt
  • Make the first line of the file #!/usr/bin/php, or see results from above
  • Run it as a web page like you would a normal php page.
  • Run it from the command line, "./my_php.php"
  • Run it from the command line, "./my_php.php > out.log" check all your 'prints' are in the log file
  • Add the above command to the cron file

 

hope that helps

 

monk.e.boy

 

Link to comment
https://forums.phpfreaks.com/topic/39551-cron-jobs/#findComment-191205
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.