Nested functions

Hi,

I am trying to write a script that will search a table for a specific part numbers previous part numbers (some have been changed several times). The table has 4 fields (partcodechangeid, partcodechangeoldpartcode, partcodechangenewpartcode, partcodechangedatechange). The result is used to form a new query which searches a stock movement table.

I am using the following function to search the table

[php]function queryoldparts($oldpartcode) {

$oldpartquery="select * from PartCodeChange where PartCodeChangeNewCode='$oldpartcode'";
	$oldpartresult=mssql_query($oldpartquery);
	$oldpartnumrows=mssql_num_rows($oldpartresult);
	if ($oldpartnumrows==0){
		//no older parts
	} else {
		//partcode has previously changed search on all
		while ($partrow=mssql_fetch_array($oldpartresult)) {
			$oldcode=$partrow['PartCodeChangeOldCode'];
		$GLOBALS["qtysql"] .= "StockAuditPartCode='$oldcode' or ";
			queryoldparts($oldcode);
		}
	}
}[/php]

and a similar function to search for any newer part codes

I call the functions from the following snippet

[php]$partquery=“select * from PartCodeChange where PartCodeChangeOldCode=’$mysqlpart’ or PartCodeChangeNewCode=’$mysqlpart’”;
$partresult=mssql_query($partquery);
$partnumrows=mssql_num_rows($partresult);
if ($partnumrows==0){
//no records in partcodechange table sql is simple
$qtysql=“select * from StockAudit where StockAuditPartCode=’$mysqlpart’ order by StockAuditId desc”;
} else {
$qtysql="select * from StockAudit where ";
//partcode has previously changed search for all other partcodes

  while ($partrow=mssql_fetch_array($partresult)) {
    $oldcode=$partrow['PartCodeChangeOldCode'];
    $newcode=$partrow['PartCodeChangeNewCode'];
    If ($oldcode==$mysqlpart) {
	$qtysql .= "StockAuditPartCode='$newcode' or ";
	querynewparts($newcode);
    } else {
	$qtysql .= "StockAuditPartCode='$oldcode' or ";
	queryoldparts($oldcode);
    }
  }[/php]

The script will not work unless i comment out the two lines calling the functions. Can anyone help? I’m still learning php as its not my first language.

Shameless bump

I looked at my code again, and realised I’d made such a hash of it. Thats what you get when your constantly distracted by customers.

I modified the functions as below, and they do get parsed correctly now, however, I’ve come across a strange problem in that the script says there are no results, but when I execute the query in Microsoft SQL Server Management Studio, I get results. Heres the modified code

[php]function queryoldparts($oldpartcode) {
$oldpartquery=“select * from PartCodeChange where PartCodeChangeNewCode=’$oldpartcode’”;
echo $oldpartquery . “
”;
$oldpartrows=mssql_num_rows($oldpartquery);
if ($oldpartrows) {
while ($row = mssql_fetch_array($oldpartquery)) {
$oldcode = $row[‘PartCodeChangeOldCode’];
$GLOBALS[“qtysql”] .= "StockAuditPartCode=’$oldcode’ or ";
queryoldparts($oldcode);
}
}
}[/php]

I’m echoing out the sql query to make sure it is correct, and then copy/paste it into SQL server management studio, and getting results. But the script doesn’t run the code within the IF statement… Anyone got any ideas?

James

Sponsor our Newsletter | Privacy Policy | Terms of Service