Page 1 of 1

Testing for failure of mssql query.

Posted: Wed Aug 23, 2006 9:32 am
by kingconnections
Ok so here is the idea, I am taking a result of a query and passing them to the next query.
It is working fine except in the event that the server being passed to the new query does not exist in the destination table. I am not sure how to test for this. I need to see these results as well.

Code: Select all

$query2= "select  distinct(computer ), targetgroup
from susdb.dbo.VLO_ComputerStatus2 
where SummarizationState =2 or SummarizationState =3 or SummarizationState =6";

$result2 = mssql_query($query2, $dbConn2);
$x="";


while ($row = mssql_fetch_assoc($result2)){
   $val = $row['computer'];
   $val2= $row['targetgroup'];
   $val3="";
   $val4="";
   $val5="";
   $val6="";

echo"-----$val----$val2<br>";


$query3 ="select owner1,owner2,owner3, location from servers.dbo.serverinfo where server ='$val' ";

$result3 = mssql_query($query3, $dbConn2);

$x++;


while ($row = mssql_fetch_assoc($result3)){
  
do some stuff

}
That is how i have it coded now. I tied doing an isset on $result3, but I did it wrong or it didn't work. I also tried a die statement.

Any ideas?

Posted: Wed Aug 23, 2006 9:41 am
by feyd
Ya fugot ta read da manuel -- You forgot to read the manual, for those at home
mssql_query() wrote:Returns: A MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.

Posted: Wed Aug 23, 2006 9:56 am
by kingconnections
I saw that. This is how I resovled my problem.

Code: Select all

$num_rows = mssql_num_rows($result3);


if ($num_rows==0){

do left out items....
}


proceed to next query

so basically if your query returns zero rows you have tested for this condition.

Is there a better way to accomplish this?

Posted: Wed Aug 23, 2006 10:03 am
by feyd

Code: Select all

if ($result3 === false)
{
  // error
}
elseif ($result3 === true)
{
  // no rows
}
else
{
  // you have rows
}

Posted: Wed Aug 23, 2006 10:44 am
by kingconnections
:D Thanks!!!!!! If this forum had rating system, I would give you a plus. Thanks again!!!