Hi I have a table in mysql:

CREATE TABLE user (id INT, name VARCHAR(250));

I query the table:

$result = mysql_query("SELECT id,name FROM user");

I gather the results:

while($row = mysql_fetch_assoc($result) $rv[] = $row;

And I return the data:

echo json_encode($rv);

The problem is that the id returns as string and not int, even before the json_encode. If i force $row['id'] = (int)$row['id']; - it works fine.

How can i force mysql to return the correct data type in the $row?


Accepted Answer

PDO->bindColumn can this

$stmt = $dbh->prepare($sql);

$stmt->bindColumn('int_col', $intVar, PDO::PARAM_INT);
$stmt->bindColumn('string_col', $strVar, PDO::PARAM_STR);



$intVar will be automatically converted to int

Written by stereofrog
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki