Mysql_query save data type created in table on return?

I have a table in mysql:

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

I am querying a table:

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

I get the results:

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

And I return the data:

echo json_encode($rv);

The problem is that the identifier is returned as a string, not int, even before json_encode. If I force $ row ['id'] = (int) $ row ['id']; - It works great.

How to make mysql return the correct data type in the string $?

10x

+5
source share
2 answers

PDO-> bindColumn can this

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

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

$stmt->execute();

$stmt->fetch(PDO::FETCH_BOUND);

$ intVar will be automatically converted to int

+2
source

, PHP <= 5.2 / MySQL (.. mysql_*, mysqli_* PDO) libmysql - C, MySQL.

, , , integer float, : " " (, ORM), "", PHP, , , .

. , , , .


PHP >= 5.3 , MySQL, mysqlnd (MySQL Native Driver) libmysql - .

"" , mysqlnd; , : PHP: , mysqlnd.


:

  • PHP 5.2 ; - .
  • PHP 5.3 - , .
+3

All Articles