Hello All,

Having a few problems with the code below and wondered if someone could shed some light on it, I have a php script running from a cron that runs every 15 mins the error I am getting tom the cron is

PHP Notice: Undefined variable: rows in /var/www/vhosts/server.com/migration_tracker/staff/5th_email.php on line 9

My code is

<?php 

include('config.inc');

$agree_date = $rows['agree_date'];

if ($agree_date > strtotime('2 week')) { 


mysql_select_db($dbname); 
$sql="SELECT * FROM db, 5th_email WHERE old_mbox_del = '0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND 6th_email_sent = '0'"; 
$result = mysql_query($sql) or die(mysql_error());

$count = 1;
while ($rows = mysql_fetch_array ($result)) {

$to  = $rows['contact_email']; 
$from = "support@domain";
$subject = "Migration Team";
$message = $rows['body'];
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers);
    if ($count % 5 == 0) {
      sleep(5);
    }
    $count++;
}
$sql="UPDATE tracker_db SET 5th_email_sent ='1' WHERE 6th_email_sent ='0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND old_mbox_del = '0'";
$result=mysql_query($sql);

}
mysql_close();
?>

I think the problem is I havn't declared $agree_dat correctly or havn't selected from the db, could this be the cuase

Cheers
Martin

Member Avatar for diafol

Your title is misleading. It's not a cron issue, is it? Which line in your code is this error attributed to. I know it says line 9, but there's nothing there in your code.

$agree_date = $rows['agree_date'];

Seems like the culprit. Where is $rows declared before this?

Sorry for the misleading title, thaught it was related to the cron as the error where being sent out by the cron deamon :)

Line 9 refered to

$agree_date = $rows['agree_date'];

as I did try an put a select all stament in but didn't work, I havn't declared $rows before it, thaught it may have been somthing like that

Member Avatar for diafol

I'm not sure what you want from us. The line mentioned will obviously fail. So what do you need to do? Run a query before that?

Sorry for the confusion, has been one of those mornings :) Yeah I have added a query to my code just about to check it, Thanks for your help

I have updated my code to this

<?php 

include('config.inc');

$sql="SELECT * FROM tracker_db";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);

$agree_date = $rows['agree_date'];

if ($agree_date > strtotime('2 week')) { 


mysql_select_db($dbname); 
$sql="SELECT * FROM db, 5th_email WHERE old_mbox_del = '0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND 6th_email_sent = '0'"; 
$result = mysql_query($sql) or die(mysql_error());

$count = 1;
while ($rows = mysql_fetch_array ($result)) {

$to  = $rows['contact_email']; 
$from = "support@domain";
$subject = "Migration Team";
$message = $rows['body'];
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers);
    if ($count % 5 == 0) {
      sleep(5);
    }
    $count++;
}
$sql="UPDATE db SET 5th_email_sent ='1' WHERE 6th_email_sent ='0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND old_mbox_del = '0'";
$result=mysql_query($sql);

}
mysql_close();
?>

I am now gertting

PHP Notice: Undefined variable: rows in /var/www/vhosts/server.com/migration_tracker/staff/5th_email.php on line 9

Think is must be somthing to do witnh the query what to you reckon

Cheers
Martin

Where's the mysql_connect() ?

Yeah have tried adding the mysql_connect still getting the same problem :(

Did you include mysql_select_db() too? Did you check for errors?

Yeah have treid mysql_select_db() and got the following

PHP Parse error: syntax error, unexpected T_STRING in /var/www/vhosts/server.com/migration_tracker/staff/5th_email.php on line 7

Show your code. It's a typo.

<?php 

include('config.inc');

mysql_connect()

mysql_select_db($dbname); 
$sql="SELECT * FROM tracker_db";
$result=mysql_query($sql);

$agree_date = $rows['agree_date'];

$rows=mysql_fetch_array($result);


if ($agree_date > strtotime('2 week')) { 


mysql_select_db($dbname); 
$sql="SELECT * FROM db, 5th_email WHERE old_mbox_del = '0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND 6th_email_sent = '0'"; 
$result = mysql_query($sql) or die(mysql_error());

$count = 1;
while ($rows = mysql_fetch_array ($result)) {

$to  = $rows['contact_email']; 
$from = "support@domain";
$subject = "Migration Team";
$message = $rows['body'];
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers);
    if ($count % 5 == 0) {
      sleep(5);
    }
    $count++;
}
$sql="UPDATE db SET 5th_email_sent ='1' WHERE 6th_email_sent ='0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND old_mbox_del = '0'";
$result=mysql_query($sql);

}
mysql_close();
?>

Line 5 is missing a semi-colon at the end, but also the parameters required to connect to your server.

Thanks, I store my connection details in config.inc sorry for all the confusion

Assuming your connection doesn't run
in config.inc you need to fill in the mysql_connect() in the above code with the variables from the file, for instance:
mysql_connect($host, $user, $password) or die(mysql_error());

Also this bit of code is backwards again:

11. $agree_date = $rows['agree_date'];
12. 
13. $rows=mysql_fetch_array($result);

You fundamentally cannot use data from an array before it has been put there.

Thanks, have made some changes still having problems, thanks for your help though

Member Avatar for diafol

Thanks, have made some changes still having problems,

Care to share?

Hello,

Yeah no probs have made a slight change but I am getting lost with it now, kind of working to a deadline :(

<?php 

include('config.inc');


$date = $row['agree_date'];

if ($date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week"); { 

mysql_select_db($dbname); 
$sql = "SELECT * FROM db, 5th_email WHERE old_mbox_del = '0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND 6th_email_sent = '0'"; 
$result = mysql_query($sql) or die(mysql_error());

$count = 1;
while ($rows = mysql_fetch_array ($result)) {

$to  = $rows['contact_email']; 
$from = "support@domain";
$subject = "Migration Team";
$message = $rows['body'];
$headers = "From:" . $from; 
mail($to,$subject,$message,$headers);
    if ($count % 5 == 0) {
      sleep(5);
    }
    $count++;
}
$sql="UPDATE db SET 5th_email_sent ='1' WHERE 6th_email_sent ='0' AND 1st_email_sent = '1' AND 2nd_email_sent = '1' AND 4th_email_sent = '1' AND 5th_email_sent = '0' AND old_mbox_del = '0'";
$result=mysql_query($sql);

}
mysql_close();
?>

Cheers
Martin

Member Avatar for diafol

Dear UTUK

You have been advised countless (it seems) times about this line...

$date = $row['agree_date'];

Unless you've placed some inappropriate code into config.inc, then that will never work as $row does not exist yet.

ok let me check, I have tried it in many ways and chnaged waht I have been advised I have put it back to the way it was cause it didnt work

Member Avatar for diafol

Just because it didn't work doesn't mean that your original code was better. I would suggest that you made incremental improvements. Use SVN or similar to keep versions, then you can dust off the best one to work on. No need to go back to square 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.