Tag: variable
variable Object MSSQL note
by z3n on Sep.25, 2009, under Notes
Problem:
return $res->Fields['field_name']->Value;
returns `variable Object`
Solution:
return intval($res->Fields['field_name']->Value);
Explanation:
There’s no explanation, this is one of those issues that happens when you deal with microsoft. Sometimes when doing a query it returns the actual value, other times it just return into a different variable type, which needs to be converted. Good thing if you caught it before writing a more complex code and then finally dealing with crypt errors :)
Bonus:
I’ve been getting some more issues even when using this conversion, things like “Object of class variant could not be converted to string php“, this happens on null values, in order to fix i been searching around and found this, which with some changes it will be better like:
-
for ($col=array(),$i=0,$j=$ms_res->Fields->Count();$i < $j;$i++) {
-
try {
-
$value=$ms_res->Fields[$i]->Value;
-
if (trim($value) != "") {
-
if (gettype($value) !== 'string') {
-
if (settype($value,"string")) {
-
$col[$ms_res->Fields[$i]->Name]=$value;
-
}
-
} else {
-
$col[$ms_res->Fields[$i]->Name]=$value;
-
}
-
} else {
-
$col[$ms_res->Fields[$i]->Name]='NULL';
-
}
-
} catch (exception $e) {
-
_r("Exception: ".$e);
-
}
-
}
..and yes you will figure out the missing functions/vars.