luke luke - 4 months ago 12
MySQL Question

php code returns int instead of a string

I'm trying to fetch data from mysql database and echo the output using


$salons_array = array();

$fetch_from_table = mysqli_query($con,"SELECT * FROM table")

or die(mysqli_error($con));

while ($row = mysqli_fetch_assoc($fetch_from_table)) {
$company['id'] = $row['id'];
$company['name'] = $row['name'];


The above returns the output:

array(2) { ["id"]=> string(1) "8"
["name"]=> string(12) "company name" }

Inside the database the
is an int, not a string. Nevertheless, the output is a string. Consequently,
formats it as a string too which is not what I want.

How do i change id from a string to an int?


Have a look at the JSON_NUMERIC_CHECK option for json_encode which will automatically convert numeric items it understands to their non-string form in the encoded string.

A small caveat, do note though that if something you expect to be a string contains only numbers for example, this will still be converted.