Req #68574 [Opn->Csd]: Change fetch_fields()'s output to reflect table definition

From: Date: Tue, 09 May 2017 10:35:38 +0000
Subject: Req #68574 [Opn->Csd]: Change fetch_fields()'s output to reflect table definition
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-209022@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68574&edit=1

 ID:                 68574
 Updated by:         fjanisze@php.net
 Reported by:        sebastianzartner at gmail dot com
 Summary:            Change fetch_fields()'s output to reflect table
                     definition
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            MySQLi related
 PHP Version:        5.6.3
-Assigned To:        
+Assigned To:        fjanisze
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2014-12-09 09:54:59] 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
        )

)



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



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


Thread (2 messages)

« previous php.bugs (#209022) next »