Click to See Complete Forum and Search --> : Difficulty recognizing null values in database


Ebola
06-15-2006, 09:05 AM
I'm trying to show values from a database using mssql, if there is a value in the shipp_attn field, I will display that, if that is missing, I will display the shipname, and if that is missing I will display the order number which I know is there.

This is the code I have for it:
if($ShipAttn != ""){
echo"<a href=\"./PendingOrders.php?OID=$OrderID\">$ShipAttn (Order Number: $OrderID)</a><br>";
}elseif($ShipName != ""){
echo"<a href=\"./PendingOrders.php?OID=$OrderID\">$ShipName (Order Number: $OrderID)</a><br>";
}else{
echo"<a href=\"./PendingOrders.php?OID=$OrderID\">(Order Number: $OrderID)</a><br>";
}

This code works fine so long as there is a <null> in the shipp_attn($ShipAttn) field, however some of the missing values simply have nothing in the field instead of <null> and my code will not recognize that as "", null, or not being set, as I have tried all seperately and together. When one of these fields comes up it will not jump to the elseif($ShipName != "") part and it will display..nothing. I have no idea why it is doing this, would anyone be able to lend a hand?

I added those (Order Number: $OrderID)'s in as a temporary solution, but I would like to have this work as I had planned out.

Webnerd
06-15-2006, 09:16 AM
Try using the strval() function:

if (strval($ShipAttn) != ""

Try also echoing out the serialized value of your variable:

echo serialize($ShipAttn)

To see what it is actually being set as

Ebola
06-15-2006, 09:18 AM
Tried it, same result.
s:1:" " is what it's being serialized as, I'll try adding (|| $ShipAttn == " ") into there and see if it catches it.

abou.hmed
06-15-2006, 09:26 AM
just try this condition
f($ShipAttn){

Frankie_CWRU
06-15-2006, 09:27 AM
Is there any consistency to what value should be stored in $ShipAttn when it is legitimately set? If so you could check it against a regular expression instead of against null.

Otherwise (a ****ty workaround, but it might work):
Always display $ShipAttn, if it is set, then you will display $ShipAttn as well as $OrderID and if it is not set then it will only display $OrderID

Ebola
06-15-2006, 09:32 AM
It worked after I changed the code to this:
if(strval(($ShipAttn) != "") && ($ShipAttn != " ")){
//blah blah
}
Although the empty variable wasn't actually displaying a space at all (I made sure of this), that code is catching it, thanks alot Webnerd.

NogDog
06-15-2006, 09:59 AM
You could try using trim():

if(trim($ShipAttn) != "")
{
// blah blah
}