openSUSE Forums > Programming/Scripting » PHP function question

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read


Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
Page 2 of 2 1 2
 
LinkBack Thread Tools Display Modes
  #11 (permalink)  
Old 25-Jan-2009, 10:39
yu210148's Avatar
Explorer Penguin
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 154
yu210148 hasn't been rated much yet
Default Re: PHP function question

Interesting because I'm already using freetds as the driver in my /etc/unixODBC/odbc.ini file to establish the ODBC socket to the MSSQL database (which the script then uses the odbc_connect() function to connect to.

Would a call to sybase_connect() in place of odbc_connect() do the trick?
Reply With Quote
  #12 (permalink)  
Old 25-Jan-2009, 12:08
yu210148's Avatar
Explorer Penguin
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 154
yu210148 hasn't been rated much yet
Default Re: PHP function question

Think I may have at least a work around for this. The result of the query is always a single value--to be sure of this I changed the SELECT DISTINCT to SELECT TOP 1--so the odbc_fetch_into() and indeed the whole while loop is unnecessary and I replaced it with

Code:
odbc_fetch_row($q);
$field = odbc_result($q, 1);
Then replace the $row[0] in the UPDATE statement with $field.

I've run the script several times without running into the canary mismatch even after taking the usleeps() out. <fingers crossed>

The same logic is used in several other functions in the script but they all return multiple values and haven't been erroring out.
Reply With Quote
  #13 (permalink)  
Old 29-Jan-2009, 11:40
elvigia's Avatar
Student Penguin
 
Join Date: Jun 2008
Location: Talcahuano,Chile
Posts: 58
elvigia hasn't been rated much yet
Wink Re: PHP function question

Quote:
Originally Posted by yu210148 View Post
Hi all,

I've got a function in one of my php scripts that periodically errors out with
Code:
[Wed Jan 21 11:20:21 2009] [error] [client 10.0.10.13] ALERT - canary mismatch on efree() - heap overflow detected (attacker '10.0.10.13', file '/srv/www/htdocs/sales_by_department.php', line 1335), referer: http://linux-aqep/sales_by_department.php

Please file a bug report at https://bugzilla.novell.com, if you have a short reproducible test case, I will take care of it.
Reply With Quote
  #14 (permalink)  
Old 09-Feb-2009, 07:50
yu210148's Avatar
Explorer Penguin
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 154
yu210148 hasn't been rated much yet
Default Re: PHP function question

Thanks I'll do that.
Reply With Quote
  #15 (permalink)  
Old 09-Feb-2009, 08:37
yu210148's Avatar
Explorer Penguin
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 154
yu210148 hasn't been rated much yet
Default Re: PHP function question

Okay, it's Bug 473915.

If you need more info let me know but I think I've pretty much covered it.
Reply With Quote
  #16 (permalink)  
Old 26-May-2009, 06:01
yu210148's Avatar
Explorer Penguin
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 154
yu210148 hasn't been rated much yet
Smile Re: PHP function question

Although this was happening less after the bug above was closed it was still coming up once and a while.

So while working on another script I came across a solution using the prepare and execute methods in the pear DB class.

Unfortunately, this led to an invalid cursor state error message from the DB.

The issue seemed to have something to do with the open database connection being reused when the function was called. The function sending another select statement caused the invalid cursor state.

So....

A separate function to get the--in this case--locations in an array and return that:
Code:
// function to get a list of locations and put them in an array then returns that array
function get_locations(){
$locationsArray = array();
require_once('DB.php');
	$dbl = DB::connect("odbc://XXXXXX:XXXXXX@/Winprism");

	// ...and check for errors.
	if (DB::iserror($dbl)) {
		die($dbl->getDebugInfo());
	};

$sqll = $dbl->prepare("
SELECT
	Location.Description
FROM
	Location
");

if (PEAR::isError($sqll)) {
    die($sqll->getDebugInfo());
}
$ql =& $dbl->execute($sqll);
if (PEAR::isError($ql)) {
    die($ql->getDebugInfo());
}




while ($ql->fetchInto($row)) {
	$Location = $row[0];
	$Location = trim($Location);
$locationsArray[] = $Location;
} // end while

return $locationsArray;

$dbl->disconnect();
} // end function definition for get_locations()
Then, instead of using a while loop as in the original version I used a foreach loop to iterate through this array. Only one connection to the ODBC database needed.

So the new version of the get_TY_Non_Merch function goes:
Code:
function get_TY_Non_Merch($Location, $FromDate, $ToDate){


require_once('DB.php');

	$dbx = DB::connect("odbc://XXXXXX:XXXXXX/Winprism");
	// ...and check for errors.
	if (DB::iserror($dbx)) {
		die($dbx->getDebugInfo());
	}

$sql = $dbx->prepare("
SELECT TOP 1
	((SUM((SalesHistoryDetail.SaleAmt)) + SUM((SalesHistoryDetail.SaleDisc))) - (SUM((SalesHistoryDetail.RtnAmt)) 
	+ SUM((SalesHistoryDetail.RtnDisc))) - (SUM((SalesHistoryDetail.SaleDisc)) - SUM((SalesHistoryDetail.RtnDisc)))) AS NonMerch
FROM
	SalesHistoryHeader
INNER JOIN
	SalesHistoryDetail
			ON
		SalesHistoryHeader.SHMID = SalesHistoryDetail.SHMID
INNER JOIN
	Location
			ON
		SalesHistoryHeader.LocationID = Location.LocationID
INNER JOIN
	SalesTypes
			ON
		SalesHistoryDetail.TypeID = SalesTypes.TypeID
WHERE
	SalesTypes.Description = 'Non-Merch'
AND
	SalesHistoryHeader.PostDate >= '$FromDate'
AND
	SalesHistoryHeader.PostDate <= '$ToDate'
AND
	Location.Description = '$Location'
");

if (PEAR::isError($sql)) {
    die($sql->getDebugInfo());
}
$q =& $dbx->execute($sql);
if (PEAR::isError($q)) {
    die($q->getDebugInfo());
}


//$q = odbc_exec($db, $sql);
require_once('DB.php');
$db2 = DB::connect("mysql://XXXXXXX:XXXXXXXX@localhost/sales_by_department");
	if (DB::iserror($db2)) {
	die($db2->getDebugInfo());
	}


while ($q->fetchInto($row)){
	$field = $row[0];
} // end while

//odbc_fetch_into($q, $row);
$sql2 = "
INSERT INTO sales_by_department.dcc_sales VALUES ( 'NULL', 'NM', '-', '$field', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL', 'NULL' )
";

// this is a custom function that does what it
// says
send_query($sql2, $db2);

disconnect($db2);
disconnect($dbx);
//odbc_close($db);
return 0;

} // end function definition for get_TY_Non_Merch()
and that's executed in the foreach loop

Code:
$locationArray = get_locations();
foreach ($locationArray as $key=>$Location){
refresh_temp_table();
get_department_list();
get_sales_data_TY($Location, $TYDateFrom, $TYDateTo);
get_sales_data_LY($Location, $LYDateFrom, $LYDateTo);
get_departments_with_dcc_sales_TY($Location, $TYDateFrom, $TYDateTo);
get_departments_with_dcc_sales_LY($Location, $LYDateFrom, $LYDateTo);
get_TY_Non_Merch($Location, $TYDateFrom, $TYDateTo);
get_LY_Non_Merch($Location, $LYDateFrom, $LYDateTo);
get_totals();

print "<td>";
print_output($fBorder, $Location, $TYDateFrom, $TYDateTo, $LYDateFrom, $LYDateTo);
print "</td>";
Reply With Quote
Reply
Page 2 of 2 1 2

Bookmarks


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




 

Search Engine Friendly URLs by vBSEO 3.3.0 RC2