Page 1 of 1

PHP failing to save report with Cron

Posted: Fri Mar 11, 2011 2:17 am
by bgm
Hi there

I have a PHP file that works perfectly when I run it in my browser (compiles a report and saves it to my server), however when run from Cron gives a totally different output. Below is the code, and below that is the output I get when run from Cron.

Code: Select all

<?php 
include("db_conn.php"); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer Appreciation Project</title>
</head>

<body>
<?php
$date = date("Y-m-d");
$time = date("h:i:s",time());
$file = $date.".html";

$fp=@fopen("active_members/".$file, 'w');
$content = "<html><head><title>Active Members Report</title></head><body>";
$content = $content . "<h1>Active Members</h1>";
$content = $content . "<p><font size=1>Report run on ". $date . " at ". $time."</font></p>";

$fetch_members = mysql_query("SELECT * FROM membership_periods WHERE (membership_periods.Start_Date <= Now() AND membership_periods.End_Date >= Now()) ORDER BY Member_Number");

if (mysql_num_rows($fetch_members) == "0")
{
$content = $content . "<p>No results were found.</p>";
} else {
$content = $content . "<table border=0 cellpadding=2 width=100%>";
$content = $content . "<tr><td><b>Member Number</b></td><td><b>Start Date</b></td><td><b>End Date</b></td><td><b>Affiliation</b></td><td><b>Voucher</b></td></tr>";

while($row = mysql_fetch_array($fetch_members))
  { 
$content = $content . "<tr><td>". $row['Member_Number'] ."</td><td>". $row['Start_Date']."</td><td>". $row['End_Date'] ."</td>"; 
  $Affiliation_ID = $row['Affiliation_Group_ID'];
  if (Affiliation_ID > 0)
  {
  $fetch_affiliation= mysql_query("SELECT * FROM affiliation_groups WHERE ID = '$Affiliation_ID'");

  $Affiliation_Group_Name	= mysql_result($fetch_affiliation, 0, "Affiliation_Group_Name");
$content = $content . "<td>". $Affiliation_Group_Name ."</td>"; 

  } else {
$content = $content . "<td> </td>"; 

  }
  
  $Member_Number = $row['Member_Number'];
  $fetch_member_id = mysql_query("SELECT ID FROM members WHERE Member_Number = '$Member_Number'");
  $Member_ID	= mysql_result($fetch_member_id, 0, "ID");
  $fetch_voucher = mysql_query("SELECT Voucher_ID FROM reward_assignments WHERE Member_ID = '$Member_ID' AND Reward_Valid_To >= Now()");
  if (mysql_num_rows($fetch_voucher) > 0)
  {
  $Voucher_ID	= mysql_result($fetch_voucher, 0, "Voucher_ID");
  $fetch_voucher_no = mysql_query("SELECT Voucher_Number FROM reward_vouchers WHERE ID = '$Voucher_ID'");
  
  $Voucher_Number	= mysql_result($fetch_voucher_no, 0, "Voucher_Number");
  }
$content = $content . "<td>". $Voucher_Number ."</td></tr>"; 
  
 $Affiliation_Group_Name 	= "";
 $Voucher_Number 			= "";
  
 }
$content = $content . "</table>"; 
}

$content = $content . "</body></html>"; 
fwrite($fp, $content);
fclose($fp);
?>
</body>
</html>
And this is the Output I get mailed to me when Cron job runs:

PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/mcrypt.ini on line 1 in Unknown on line 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer Appreciation Project</title>
</head>

<body>

PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Notice: Undefined variable: Voucher_Number in /var/www/reports/active_members.php on line 63
PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Notice: Use of undefined constant Affiliation_ID - assumed 'Affiliation_ID' in /var/www/reports/active_members.php on line 40
PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/reports/active_members.php on line 75
PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/reports/active_members.php on line 76


</body>
</html>

Any assistance would be much appreciated.

Re: PHP failing to save report with Cron

Posted: Fri Mar 11, 2011 10:25 am
by pickle
The command line invocation and Apache connected versions of PHP can use different php.ini files. These files can have different error reporting levels. Likely the version of PHP that runs when you view this file on the web has been set up to not report notices, but the CLI version has. That's why you only get those notices when running via cron. Look on line 40 - you're missing an opening $

As far as the fwrite() & fclose() errors - that's because fopen() is returning FALSE. It would output an error to tell you why, but you've put '@' in front of it, supressing errors. Take that '@' off & it will likely tell you the file wasn't found. This is because the current working directory is likely different depending on whether you're using the CLI or web versions of PHP.

Since there's no whizbang (!#..or is it #!?), you're likely not executing the file directly, and likely have something like /etc/php5 /path/to/file in your cron file. That will change the current working directory. You'll need to specify the full path to the file you want to open.