Req #68574 [NEW]: Change fetch_fields()'s output to reflect table definition

From: Date: Tue, 09 Dec 2014 09:55:00 +0000
Subject: Req #68574 [NEW]: Change fetch_fields()'s output to reflect table definition
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188971@lists.php.net to get a copy of this message
From:             sebastianzartner at gmail dot com
Operating system: 
PHP version:      5.6.3
Package:          MySQLi related
Bug Type:         Feature/Change Request
Bug description:Change fetch_fields()'s output to reflect table definition

Description:
------------
As described in bug 49961 the 'length' value within the output of
mysqli_result::fetch_fields does not always reflect what the user
expects.
Because there is no info in the documentation about fetch_fields()
depending on the default client character set (see bug 68573), people
may expect to get the length they defined for a column.

Though if the default charset is set to a multibyte charset like UTF-8,
'length' will be a multiple of the field length set by the user.

Instead of letting 'length' and 'charsetnr' depend on the default
charset, they should be set to the values defined for the specific
column.
Or, if you're worried that this breaks existing applications, you should
add a new return value (e.g. 'field_length') reflecting the actual
length, or at least include the info about the number of bytes a charset
character allocates, so people can calculate the field length by
themselves.

Test script:
---------------
SQL:
----
CREATE TABLE test (
  string varchar(10) NOT NULL,
  PRIMARY KEY (string)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci;

PHP:
----
$dbh = mysqli_connect("localhost", "user", "pass", "test",
3306);
$query = mysqli_query($dbh, "select * from test");
$fields = $query->fetch_fields();
print_r($fields);

Expected result:
----------------
With 'length' and 'charsetnr' reflecting the actual values set for the
column:
Array
(
    [0] => stdClass Object
        (
            [name] => string
            [orgname] => string
            [table] => test
            [orgtable] => test
            [def] => 
            [db] => test
            [catalog] => def
            [max_length] => 0
            [length] => 10
            [charsetnr] => 8
            [flags] => 20483
            [type] => 253
            [decimals] => 0
        )

)

With a new 'field_length' value representing the actual length of the
field:
Array
(
    [0] => stdClass Object
        (
            [name] => string
            [orgname] => string
            [table] => test
            [orgtable] => test
            [def] => 
            [db] => test
            [catalog] => def
            [max_length] => 0
            [length] => 30
            [charsetnr] => 33
            [flags] => 20483
            [type] => 253
            [decimals] => 0
            [field_length] => 10
        )

)

With a new 'charset_bytes' value representing the number of bytes the
charset uses:
Array
(
    [0] => stdClass Object
        (
            [name] => string
            [orgname] => string
            [table] => test
            [orgtable] => test
            [def] => 
            [db] => test
            [catalog] => def
            [max_length] => 0
            [length] => 30
            [charsetnr] => 33
            [flags] => 20483
            [type] => 253
            [decimals] => 0
            [charset_bytes] => 3
        )

)


Actual result:
--------------
Array
(
    [0] => stdClass Object
        (
            [name] => string
            [orgname] => string
            [table] => test
            [orgtable] => test
            [def] => 
            [db] => test
            [catalog] => def
            [max_length] => 0
            [length] => 30
            [charsetnr] => 33
            [flags] => 20483
            [type] => 253
            [decimals] => 0
        )

)


-- 
Edit bug report at https://bugs.php.net/bug.php?id=68574&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=68574&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=68574&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=68574&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=68574&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=68574&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=68574&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=68574&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=68574&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=68574&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=68574&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=68574&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=68574&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=68574&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68574&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=68574&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=68574&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=68574&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68574&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=68574&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=68574&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#188971) next »