?
Solved

Cron job using PHP

Posted on 2011-10-14
8
Medium Priority
?
371 Views
Last Modified: 2012-05-12
Hi I have the following php script
<?php

$dbHost             = 'localhost';
$dbUsername       = 'rafale3_rcleon';
$dbPassword       = 'SXX';
$dbName            = 'rafale3_TCBPro';

$Connection = @mysql_connect(localhost, rafale3_rcleon, SXX) or die ("Unable to connect to MySQL RCL");

$SelectDB = @mysql_select_db(rafale3_TCBPro, $Connection) or die ("Unable to select database");

$Today = date("Y-m-d");
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($Today)) . " -1 year");
$OneYearFromContractDate = date('Y-m-d', $dateOneYearAdded);
$SQL = "
  UPDATE PRO_CD SET
  Status = 'I'
  WHERE ContractDate < '$OneYearFromContractDate'
  AND Paid = 'Y'
";

mysql_query($SQL) or die('Error, query failed');

?>

my cron command is: /home/rafale3/public_html/pro/admin/proc/UpdateStatusOfPro.php

I keep getting the following errors:home/rafale3/public_html/pro/admin/proc/UpdateStatusOfPro.php: line 2: syntax error near unexpected token `localhost,'
/home/rafale3/public_html/pro/admin/proc/UpdateStatusOfPro.php: line 2: `mysql_connect(localhost, rafale3_rcleon, SXX) or die ("Unable to connect to MySQL RCL");'


Any help will be appriciated

Thanks

Rafael
0
Comment
Question by:rcleon
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 7

Expert Comment

by:ziceva
ID: 36970618
add

#!/usr/bin/php

on the first line

(if the php binary is not located in the /usr/bin/ folder you can type "whereis php" in a terminal to find out where is it located)
0
 

Author Comment

by:rcleon
ID: 36970674
Thanks

here is the new file:
<?php
#!/usr/bin/php
require('../../DBC.php');
require('../../TBL.php');


$Today = date("Y-m-d");
$dateOneYearAdded = strtotime(date("Y-m-d", strtotime($Today)) . " -1 year");
$OneYearFromContractDate = date('Y-m-d', $dateOneYearAdded);

$SQL = "
  UPDATE $Table1 SET
  Status = 'I'
  WHERE ContractDate < '$OneYearFromContractDate'
  AND Paid = 'Y'
";

mysql_query($SQL) or die('Error, query failed');
?>

Here is the new error:

/bin/sh: home/rafale3/public_html/pro/admin/proc/UpdateStatusOfPro.php: No such file or directory
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 36970751
mysql_connect(localhost, rafale3_rcleon, SXX should probably be:

mysql_connect($dbHost, $dbUsername, $dbPassword) or die....
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:ziceva
ID: 36970836
nope... on the first line, BEFORE the tag

#!/usr/bin/php
<?
....
0
 

Author Comment

by:rcleon
ID: 36970865
Thanks

I will try next
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36971337
You might also want to put this into a cron job and run it.  It will email you the phpinfo() that the cron job sees.  This is likely to be different from what web scripts see.  Compare the paths and variables closely.
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ob_start();
phpinfo();
$msg = ob_get_clean();
mail('You@Your.org', 'CRON OUTPUT', $msg);

Open in new window

0
 
LVL 7

Expert Comment

by:Sandeep Kothari
ID: 36974903
why the strings passed in the function are not in single or double quotes ...
what I mean for the below lines in your code... put stings like localhost, rafale3_rcleon, SXX in single or double quotes....
$Connection = @mysql_connect(localhost, rafale3_rcleon, SXX) or die ("Unable to connect to MySQL RCL");

$SelectDB = @mysql_select_db(rafale3_TCBPro, $Connection) or die ("Unable to select database");

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36975793
Regarding this:
$Connection = @mysql_connect(localhost, rafale3_rcleon, SXX) or die ("Unable to connect to MySQL RCL");

PHP will first try to interpret the unquoted strings as defined constants.  It will fail, and PHP will issue a notice but you may not see the notice unless you have raised error_reporting(E_ALL) and that is something you always want to do.

After the failure and the notice. PHP will next try to interpret the unquoted strings as single-quoted, literal strings.

Man page here:
http://php.net/manual/en/language.types.string.php

Best regards, ~Ray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question