php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68788 PDO-MySQL fetch() does not return correct type of DB field
Submitted: 2015-01-10 14:55 UTC Modified: -
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: jon dot dufresne at gmail dot com Assigned:
Status: Open Package: PDO MySQL
PHP Version: 5.5.20 OS: Linux
Private report: No CVE-ID:
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jon dot dufresne at gmail dot com
New email:
PHP Version: OS:

 

 [2015-01-10 14:55 UTC] jon dot dufresne at gmail dot com
Description:
------------
When fetching data from a MySQL database, the type of the field is ignored and the column data is always returned as a string. Constrast this with the PostgreSQL driver which does return the type of the field.

The test script below demonstrates an integer field is retruned as a string for MySQL but properly returned as an int for PostgreSQL.

Test script:
---------------
<?php

$db = new PDO('pgsql:dbname=test', 'postgres');

$db->exec('DROP TABLE IF EXISTS test_table');
$db->exec('CREATE TABLE test_table (field integer)');

$db->exec('INSERT INTO test_table VALUES (5)');
$stmt = $db->query('SELECT * FROM test_table');
var_dump($stmt->fetch());



$db = new PDO('mysql:dbname=test', 'postgres');

$db->exec('DROP TABLE IF EXISTS test_table');
$db->exec('CREATE TABLE test_table (field integer)');

$db->exec('INSERT INTO test_table VALUES (5)');
$stmt = $db->query('SELECT * FROM test_table');
var_dump($stmt->fetch());


Expected result:
----------------
array(2) {
  'field' =>
  int(5)
  [0] =>
  int(5)
}
array(2) {
  'field' =>
  int(5)
  [0] =>
  int(5)
}


Actual result:
--------------
array(2) {
  'field' =>
  int(5)
  [0] =>
  int(5)
}
array(2) {
  'field' =>
  string(1) "5"
  [0] =>
  string(1) "5"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC