<?php
error_reporting(E_ALL ^ E_NOTICE);

define('DB_SERVER', "localhost");
define('DB_USER', "nurturem_root");
define('DB_PASSWORD', "+faH11M@dBxP");
define('DB_TABLE', "nurturem_smsystem");

// The procedural way
$mysqli = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE);

if (mysqli_connect_errno($mysqli)) {
    trigger_error('Database connection failed: '  . mysqli_connect_error(), E_USER_ERROR);
}

# execute the SQL query and return records
$query = "SELECT id
         FROM transaction
         WHERE NOW() > (subscription_end_on +  INTERVAL 90 DAY)
         AND is_active = '1'";

$result = mysqli_query($mysqli, $query) or trigger_error("Query Failed! SQL: $query - Error: ". mysqli_error($mysqli), E_USER_ERROR);

# fetch tha data from the database
$arrIds = array();

if($result) {
	while($row = mysqli_fetch_assoc($result)) {
		array_push($arrIds, $row['id']);
	}
} 

$strIds = "";
# Update the value of Expiry only after the array has some values.
if (count($arrIds) > 0) {
    #Make the array to string by adding comma separated values.
    $strIds = implode(",", $arrIds);
	$strUpdateQuery = "UPDATE transaction
                  		SET is_active = '0'
              			WHERE NOW() > (subscription_end_on +  INTERVAL 90 DAY)";

    if (mysqli_query($mysqli, $strUpdateQuery)) {
    echo "Record updated successfully";
	} else {
	    echo "Error updating record: " . mysqli_error($conn);
	}
}
echo $strIds;
# close the connection
mysqli_close($mysqli);

$to = "phani787@gmail.com";
$subject = "This is subject";
$message = "This is HTML message.".$strIds;
$header = "From: noreply@example.com\r\n";
$header.= "MIME-Version: 1.0\r\n";
$header.= "Content-Type: text/plain; charset=utf-8\r\n";
$header.= "X-Priority: 1\r\n";
mail ($to,$subject,$message,$header);

?>