Bug #68788 [Opn->Csd]: PDO-MySQL fetch() does not return correct type of DB field

From: Date: Thu, 08 Apr 2021 19:40:19 +0000
Subject: Bug #68788 [Opn->Csd]: PDO-MySQL fetch() does not return correct type of DB field
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233329@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68788&edit=1

 ID:                 68788
 Updated by:         dharman@php.net
 Reported by:        jon dot dufresne at gmail dot com
 Summary:            PDO-MySQL fetch() does not return correct type of DB
                     field
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            PDO MySQL
 Operating System:   Linux
 PHP Version:        5.5.20
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2015-01-10 14:55:03] 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"
}



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=68788&edit=1


Thread (2 messages)

« previous php.bugs (#233329) next »