php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #68574 Change fetch_fields()'s output to reflect table definition
Submitted: 2014-12-09 09:54 UTC Modified: 2017-05-09 10:35 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: sebastianzartner at gmail dot com Assigned: fjanisze (profile)
Status: Closed Package: MySQLi related
PHP Version: 5.6.3 OS:
Private report: No CVE-ID: None
 [2014-12-09 09:54 UTC] sebastianzartner at gmail dot com
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
        )

)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-09 10:35 UTC] fjanisze@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: fjanisze
 [2017-05-09 10:35 UTC] fjanisze@php.net
I see the problem, but PHP do not change any of the values received from the server, the length you have is what we get. For additional meta data the server shall be required for modification, not PHP.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 13:01:30 2024 UTC