I'm doing a select all SQL query on a table and running it in a prepared query in PHP. I'm then echoing the result inside a json_encode. The result is putting every value as a string, even the row ID which is an INT. How do you keep the original value types ?
$sql = "SELECT * FROM `Type`";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
The output is as follows:
[{"ID":"1","Type":"Classic Starters","Description":""},{"ID":"2","Type":"Special Starters","Description":""}]
The desired output is as follows:
[{"ID":1,"Type":"Classic Starters","Description":""},{"ID":2,"Type":"Special Starters","Description":""}]
Thanks in advance <3
(int)$row['ID']I would just cast it to an int. I don't like relying on obscure settings as it hampers portability. If you cast it then you are sure it's an int. You may be able to cast it in the SELECT part of the query w3schools.com/sql/func_mysql_cast.aspSELECT CAST(ID as UNSIGNED)