I ran into this probelm with my query
The following are my codes:
$conn=mssql_connect($DataSource,$UserId,$Password)
or die("Couldn't connect to server.");
mssql_select_db($InitialCatalog,$conn);
$sSQL=
"IF '$sAgentID' IN (SELECT AgentID FROM telecom1.Agents) "."\r\n".
" SELECT pathLogoImage, "."\r\n".
" CompanyName, "."\r\n".
" Address, "."\r\n".
" City, "."\r\n".
" State, "."\r\n".
" Zip, "."\r\n".
" Phone, "."\r\n".
" Fax, "."\r\n".
" CompanyEmail, "."\r\n".
" CompanyURL "."\r\n".
" FROM telecom1.Agents "."\r\n".
" WHERE AgentID = '$sAgentID'"."\r\n".
"ELSE "."\r\n".
" SELECT TOP 1 "."\r\n".
" pathLogoImage, "."\r\n".
" CompanyName, "."\r\n".
" Address, "."\r\n".
" City, "."\r\n".
" State, "."\r\n".
" Zip, "."\r\n".
" Phone, "."\r\n".
" Fax, "."\r\n".
" CompanyEmail, "."\r\n".
" CompanyURL "."\r\n".
" FROM telecom1.Agents "."\r\n".
" WHERE Left(AgentID,3) = "."'".substr($sAgentID,0,3)."'"."\r\n".
" ORDER BY AgentID"."\r\n";
$rs=mssql_query($sSQL,$conn);
echo mssql_result($rs,0,"CompanyName");
I got the following error:
mssql_result(): supplied argument is not a valid MS SQL-result resource
However if I take out the "if-else" statement and only use top half of the query everything is fine as follows:
$sSQL=
" SELECT pathLogoImage, "."\r\n".
" CompanyName, "."\r\n".
" Address, "."\r\n".
" City, "."\r\n".
" State, "."\r\n".
" Zip, "."\r\n".
" Phone, "."\r\n".
" Fax, "."\r\n".
" CompanyEmail, "."\r\n".
" CompanyURL "."\r\n".
" FROM telecom1.Agents "."\r\n".
" WHERE AgentID = '$sAgentID'"."\r\n";
the $sSQL string compile like the followig in html source:
IF 'ufo0008' IN (SELECT AgentID FROM telecom1.Agents)
SELECT pathLogoImage,
CompanyName,
Address,
City,
State,
Zip,
Phone,
Fax,
CompanyEmail,
CompanyURL
FROM telecom1.Agents
WHERE AgentID = 'ufo0008'
ELSE
SELECT TOP 1
pathLogoImage,
CompanyName,
Address,
City,
State,
Zip,
Phone,
Fax,
CompanyEmail,
CompanyURL
FROM telecom1.Agents
WHERE Left(AgentID,3) = 'ufo'
ORDER BY AgentID
I have run the above query in a sql console against the same database and it returns the correct result.