Jump to content

SeanAC

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

SeanAC's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Okay, so I have tried to have a go at working out how to do this myself using google... I think I may have made the problem worse I am getting the error "PHP Fatal error: Only variables can be passed by reference in /home/sqlqueries/Test.php on line 45". It is probably fair to say I have no idea what I am doing at the moment. Frankly, I am still at the stage on PHP where everything looks like another language. Help anyone? At the moment, it just sends me an e-mail with an empty spreadsheet... along with an error in PuTTY. <?php putenv("TNS_ADMIN=/usr/lib/oracle/11.2/client64"); require_once "DB.php"; @$DB = oci_connect('#####','#####','domain'); if (DB::isError($DB)) { echo 'Cannot connect to database: ' . $DB->getMessage(); } else { $strPath = "domain"; $filName = "customer.csv"; $objWrite = fopen($strPath."/".$filName, "w"); $fromdate = strtotime("last Monday"); $todate = strtotime("last Sunday"); if($todate < $fromdate) { // subtract another week $fromdate = strtotime("-1 weeks", $fromdate); } $fromdate = date('d/m/Y', $fromdate); $todate = date('d/m/Y', $todate); $body = ""; $body .= "<h3>For period " . $fromdate . " to " . $todate . "</h3>\n"; $Query = "SELECT DCONSUMER, DDATE, DTYPE, DAMT/100 AS DAMT, SUBSTR(DWHO,0,INSTR(DWHO,':')-1) AS DWHO, DREF, DEXTDESC FROM UTILITY.DEBT WHERE SUBSTR(DWHO,0,INSTR(DWHO,':')-1) IN ( SELECT ID FROM UTILITY.USERS WHERE USE_SEN = MANAGER' ) AND DTYPE NOT IN ('CHG1','CHG2','CHG3','CHG4') AND DDATE >= TO_DATE('".$fromdate."','dd/mm/yyyy') AND DDATE <= TO_DATE('".$todate."','dd/mm/yyyy') ORDER BY DAMT"; $objQuery = oci_parse($DB, $Query); while($objResult = oci_fetch_all($objQuery, OCI_BOTH)) { fwrite($objWrite, "\"$objResult[DCONSUMER]\",\"$objResult[DDATE]\",\"$objResult[DTYPE]\",\"$objResult[DAMT]\",\"$objResult[DWHO]\",\"$objResult[DREF]\",\"$objResult[DEXTDESC]\" \n"); } fclose($objWrite); //*************** Send Email ***************// $strTo = "[email protected]"; $strSubject = "CSV Report $fromdate - $todate"; $strMessage = "Download $filName for CSV Report for period $fromdate to $todate"; //*** Uniqid Session ***// $strSid = md5(uniqid(time())); $strHeader = ""; $strHeader .= "From: Automation <[email protected]>\nReply-To: [email protected]\n"; $strHeader .= "Bcc: [email protected]"; $strHeader .= "MIME-Version: 1.0\n"; $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n"; $strHeader .= "This is a multi-part message in MIME format.\n"; $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-type: text/html; charset=windows-874\n"; // or UTF-8 // $strHeader .= "Content-Transfer-Encoding: 7bit\n\n"; $strHeader .= $strMessage."\n\n"; $strContent1 = chunk_split(base64_encode(file_get_contents($strPath."/".$filName))); $strHeader .= "--".$strSid."\n"; $strHeader .= "Content-Type: application/octet-stream; name=\"".$filName."\"\n"; $strHeader .= "Content-Transfer-Encoding: base64\n"; $strHeader .= "Content-Disposition: attachment; filename=\"".$filName."\"\n\n"; $strHeader .= $strContent1."\n\n"; $flgSend = @mail($strTo,$strSubject,null,$strHeader); // @ = No Show Error // } ?>
  2. Hi Everyone, Firstly, I would like to make the following advanced apologies; [*]Please accept my apologies if this question has been asked before. If you could point me to the correct place I would be happy to read there. Thus far I have been unable to locate anything which answers this directly. [*]Please accept my apologies if I seem somewhat ignorant with regards to PHP. I am very much a beginner here, so go light on me . I have a piece of PHP code which basically runs an SQL Query, and prints the results in HTML onto an e-mail. It then e-mails the contents to me. I would prefer this to instead print the data into a CSV file and e-mail that file to me. Essentially what I want the code to do is: [*]Run an SQL Query on an Oracle database, to retrieve data [*]Export the data into a CSV file - this file does not need to be kept server side [*]E-mail this CSV File The current code I have (which prints directly into the body of the e-mail) is as below. I assume it would not be too much more difficult to put this into CSV. As this relates to company data I have substituted certain references in the data below to remove potentially sensitive references. <?php putenv("TNS_ADMIN=/usr/lib/oracle/11.2/client64"); require_once "DB.php"; @$DB = DB::connect('oci8://#####:#####@domain'); if (DB::isError($DB)) { echo 'Cannot connect to database: ' . $DB->getMessage(); } else { $fromdate = strtotime("last Monday"); $todate = strtotime("last Sunday"); if($todate < $fromdate) { // subtract another week $fromdate = strtotime("-1 weeks", $fromdate); } $fromdate = date('d/m/Y', $fromdate); $todate = date('d/m/Y', $todate); $body = ""; $body .= "<h3>For period " . $fromdate . " to " . $todate . "</h3>\n"; $query = "SELECT DCONSUMER, DDATE, DTYPE, DAMT/100 AS DAMT, SUBSTR(DWHO,0,INSTR(DWHO,':')-1) AS DWHO, DREF, DEXTDESC FROM UTILITY.DEBT WHERE SUBSTR(DWHO,0,INSTR(DWHO,':')-1) IN ( SELECT ID FROM UTILITY.USERS WHERE USE_SEN = 'MANAGER' ) AND DTYPE NOT IN (CHG1',CHG2','CHG3','CHG4') AND DDATE >= TO_DATE('".$fromdate."','dd/mm/yyyy') AND DDATE <= TO_DATE('".$todate."','dd/mm/yyyy') ORDER BY DAMT"; $result = $DB->query($query); $body .= "<table cellspacing='3'\n"; $body .= "<font size='2'>\n"; $body .= "<tr><th bgcolor='#AAAAAA'>CONSUMER</th><th bgcolor='#AAAAAA'>DATE</th><th bgcolor='#AAAAAA'>TYPE</th><th bgcolor='#AAAAAA'>AMT</th><th bgcolor='#AAAAAA'>WHO</th><th bgcolor='#AAAAAA'>REF</th><th bgcolor='#AAAAAA'>EXTDESC</th></tr>\n"; while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { $body .= "<tr><td>" . $row['DCONSUMER'] . "</td><td>" . $row['DDATE'] . "</td><td>" . $row['DTYPE'] . "</td><td>" . $row['DAMT'] . "</td><td>" . $row['DWHO'] . "</td><td>" . $row['DREF'] . "</td><td>" . $row['DEXTDESC']. "</td></tr>\n"; } $body .= "</font>\n"; $body .= "</table>\n"; #print $body; $to = ""; $subject = "Automated Query: (".$fromdate." to ".$todate.")"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Automated Scripts <[email protected]>' . "\r\n"; $headers .= 'To: [email protected]' . "\r\n"; $headers .= 'Cc: [email protected]' . "\r\n"; mail($to, $subject, $body, $headers); } ?> Your assistance is much appreciated. Cheers, Sean
×
×
  • 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.