So originally I used mysqli to connect to the database but now I'm attempting to implement PDO.
Previously when using mysqli I'd do something like this:
$sql = "select * from companies";
$statement = $this->conn->prepare($sql);
$statement->execute();
$result = $statement->get_result();
$companies = $result->fetch_assoc();
and this would fetch the associative values. Phone numbers would be stored as varchar inside mysql so the phone number would return as a string as apposed to a number.
Now, when I try to use PDO like so:
$sql = "SELECT * FROM companies";
try {
$db = new db();
$db = $db->connect();
$stmt = $db->query($sql);
$companies = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($companies);
}
catch (Exception $ex) {
echo '{"error": {"text": '.$ex->getMessage().'}';
}
all values would be returned as strings.
I tried echo json_encode($companies, JSON_NUMERIC_VALUE);
but I have issues with postal codes and phone numbers starting with 0, where the 0 just dissapears off of the front.
I thought about looping through the values and assigning the necessary type to the value, but it seems a little long winded.