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: 2021-04-08 19:40 UTC
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: nikic (profile)
Status: Closed Package: PDO MySQL
PHP Version: 5.5.20 OS: Linux
Private report: No CVE-ID: None
 [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

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-08 19:40 UTC] dharman@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2021-04-08 19:40 UTC] dharman@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

This has been fixed by Nikita in PHP 8.1. See https://github.com/php/php-src/commit/c18b1aea289e8ed6edb3f6e6a135018976a034c6
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC