Jump to content

PHP failing to save report with Cron


brucegm

Recommended Posts

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.

 

<?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.

 

Link to comment
Share on other sites

Why when this is running as a cron job are you outputting all that HTML?  Also, some of those errors are relating to the file handle, so when you're debugging, it might be handy to NOT suppress the errors.

 

Change this

$fp=@fopen("active_members/".$file, 'w');

To this

$fp=fopen("active_members/".$file, 'w');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.