Testing for failure of mssql query.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Testing for failure of mssql query.

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if ($result3 === false)
{
  // error
}
elseif ($result3 === true)
{
  // no rows
}
else
{
  // you have rows
}
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

:D Thanks!!!!!! If this forum had rating system, I would give you a plus. Thanks again!!!
Post Reply