Prepared queries in mysqli return numeric database columns as PHP numbers, as does the mysqlnd driver using the MYSQLI_OPT_INT_AND_FLOAT_NATIVE option.
I am modernizing a legacy codebase to use parameterized queries. Other areas of the code rely on the query results having string data types, e.g.:
if ($object->isTrue() === '1') { }
The best solution I've come up to make the output match the expectations of the existing code is to manually cast each result back to strings:
$data = [];
while ($row = $result->fetch_assoc()) {
array_walk($row, function(&$field) { $field = (string) $field; });
$data[] = $row;
}
Is there a more efficient/effective way to do this?