worldask
worldask

Reputation: 1837

laravel/php response different json data with extra double quotes in different environments

in my mac

$result = DB::select("select count(*) as count from table where id=1");
return Response::json($result[0]->count);

I got 0 in js

// PHP 5.4.24  
0

but when I published my codes into Centos, I got "0" even ""0"" in js

// PHP 5.5.14
"0" //(sometimes ""0"")

They are totally same codes. What's wrong?


Edit:

I found another problem I think it's related

// mac, PHP 5.4.24, Lavavel 4.2
Auth::user()->is_admin === 1  // true

// centos, PHP 5.5.14, Lavavel 4.2
Auth::user()->is_admin === 1  // false
Auth::user()->is_admin == 1  // true

column type of is_admin is int(11)

Upvotes: 1

Views: 1499

Answers (2)

worldask
worldask

Reputation: 1837

I solved it myself. Because I used php-mysql but not php-mysqlnd. php-mysqlnd returns native data type from mysql, but php-mysql returns some data types like int/float as string.

so I

`yum remove php-mysql'

then

'yum install php-mysqlnd'

everything goes fine!

Upvotes: 0

Antoine Augusti
Antoine Augusti

Reputation: 1608

You may want to perform a numeric check when returning the JSON to have the expected type.

return Response::json($result[0]->count, 200, [], JSON_NUMERIC_CHECK);

Upvotes: 1

Related Questions