0

I had this line of query

$sql = " SELECT c.name, IF(a.addressid IS NOT NULL,0,1) AS checked
FROM customers c
LEFT JOIN addresses a 
ON c.customerid = a.customerid
WHERE customerid = 123 ";

$array     = array();
$GetResult = $Obj->FetchData($sql, $Obj->DEFAULT_PDO_CONNECTIONS);
while ($row = $GetResult->fetch()) {
    $array[] = $row;
}
header("Content-type: application/json");
$result = json_encode($array);
echo $result;

when I echo in JSON format it show my checked output as a string. And I wonder by default what data type for alias checked? And how can I change into integer data type?

3 Answers3

1

Thanks for your answer, yes by default it already in data type number, the problem in json format. It produce my checked as string. So I found the answer here PHP json_encode encoding numbers as strings.

$result = json_encode($array, JSON_NUMERIC_CHECK);

0

i have added a convert function of that column to represent an integer type. hopefully that will translate to your desired result

SELECT c.name, Convert(IF(a.addressid IS NOT NULL,0,1), unsigned integer) AS checked
FROM customers c
LEFT JOIN addresses a 
ON c.customerid = a.customerid
WHERE customerid = 123
Ryan Tan
  • 344
  • 2
  • 11
0

Use cast or convert, full documentation.

So it would be similar to:

SELECT c.name, cast(if(a.addressid is not null, 0, 1) as unsigned) as checked
cjb110
  • 1,310
  • 14
  • 30