Click to See Complete Forum and Search --> : PHP--Testing for a Null Record from a recordset-- Help


mattv10
05-24-2007, 10:49 AM
Hi everyone..

I have a strange issue that has been very confusing to me.. Currently I am looping through a list of servers, connecting to each server and executing a query that will return one value. This value is getting placed into an excel spreadsheet using XML.. So hopefully you can picture that.. Its just one big loop doing all this..

I started to have this issue where I was getting XML errors where it said I was missing an end tag.. I knew I was not missing an end tag because I checked the code over so many times.. So, I logged into each server and ran the query in SQL Server hoping I will narrow down the problem.. I notice that 5 of my "environments" are returning NULL as the recordset...

So I went to my code and added some logic to test for a NULL value in my recordset.. I still had the same issue.. (Missing the XML end tag) Now, to verify these nulls were causing the issue I wrote some logic to print nothing in the Excel cell if the server names = (the specific servers).. And it worked!!!

What I would like to know is how can I test for this a more generic way? I tried the following functions in PHP..
--isset
--is_null
--empty

Nothing seems to work.. Any Ideas?? Oh by the way I querying from Microsoft SQL Server 2000 using an ADO Connection..

Any insight would be appreciated.. If you need anymore information I will be happy to give it to you...

Thx!

NogDog
05-24-2007, 12:05 PM
You would probably be better off checking for it in your query, such as adding a condition to your query's WHERE clause:

$sql = "SELECT field1 FROM table1 WHERE field2 = '$id' AND field1 NOT NULL";

mattv10
05-24-2007, 12:38 PM
Thanks for your idea. However, when my query is already returning one value to begin with and its NULL if I try to select it where it's NOT NULL then won't it still return NULL since there is nothing to return??

NogDog
05-24-2007, 08:44 PM
Thanks for your idea. However, when my query is already returning one value to begin with and its NULL if I try to select it where it's NOT NULL then won't it still return NULL since there is nothing to return??
No, in that case the row would not be returned at all.

If you still want the row returned, but just want to know if that particular value is null, then you might need to do something like:

$sql = "SELECT field1, IFNULL(field2, 'NULL') AS f2 FROM table1 WHERE field1 = '$id'";
$result = mysql_query($sql) or die(mysql_error() . "<br />$sql");
while($row = mysql_fetch_assoc($result))
{
if($row['f2'] == 'NULL') // note we're comparing to the string 'NULL', not a PHP null value
{
// field2 was null
}
else
{
// field2 has a non-null value so you can use $row['f2'] to get it
}
}

You can change the 2nd parameter of the INFULL() SQL function to whatever value you would prefer, such as an empty string '' if that makes more sense to you.