php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49961 Value of "length" column for fetch_fields() is incorrect.
Submitted: 2009-10-22 18:02 UTC Modified: 2009-11-04 20:39 UTC
From: aavolkoff at gmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: 5.2.11 OS: Linux l9.in-solve.ru 2.6.26-r9-s
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: aavolkoff at gmail dot com
New email:
PHP Version: OS:

 

 [2009-10-22 18:02 UTC] aavolkoff at gmail dot com
Description:
------------
Value of "length" column for fetch_fields() is incorrect.

eg: MySQL version: 5.0.37

Reproduce code:
---------------
I have table "Users":
name = "id"; type = "INT"; length = 11.
name = "name"; type = "VARCHAR"; length = 200.
name = "description"; type = "VARCHAR"; length = 200.

Code:
//$mysqli defined earlier
$query        = "SELECT * FROM `Users` LIMIT 0";
$query_result = $mysqli->real_query($query);
if ($query_result)
{
 $mysqli_result = $mysqli->use_result();
 $mysqli_fields = $mysqli_result->fetch_fields();
 $mysqli_result->free_result();
 print_r($mysqli_fields);
}

Expected result:
----------------
Array ( 
[0] => stdClass Object ( 
[name] => id
[orgname] => id 
[table] => Users
[orgtable] => Users 
[def] => 
[max_length] => 0 
[length] => 11 
[charsetnr] => 63 
[flags] => 49667
 [type] => 3 
[decimals] => 0 ) 

[1] => stdClass Object ( 
[name] => name 
[orgname] => name 
[table] => Users
[orgtable] => Users
[def] => 
[max_length] => 0 
[length] => 200 
[charsetnr] => 33 
[flags] => 0 
[type] => 253 
[decimals] => 0 ) 

[2] => stdClass Object ( 
[name] => description 
[orgname] => description 
[table] => Users
[orgtable] => Users
[def] => 
[max_length] => 0 
[length] => 200 
[charsetnr] => 33 
[flags] => 0 
[type] => 253 
[decimals] => 0 ) 
)

Actual result:
--------------
Array ( 
[0] => stdClass Object ( 
[name] => id
[orgname] => id 
[table] => Users
[orgtable] => Users 
[def] => 
[max_length] => 0 
[length] => 11 
[charsetnr] => 63 
[flags] => 49667
 [type] => 3 
[decimals] => 0 ) 

[1] => stdClass Object ( 
[name] => name 
[orgname] => name 
[table] => Users
[orgtable] => Users
[def] => 
[max_length] => 0 
[length] => 600 
[charsetnr] => 33 
[flags] => 0 
[type] => 253 
[decimals] => 0 ) 

[2] => stdClass Object ( 
[name] => description 
[orgname] => description 
[table] => Users
[orgtable] => Users
[def] => 
[max_length] => 0 
[length] => 600 
[charsetnr] => 33 
[flags] => 0 
[type] => 253 
[decimals] => 0 ) 
)

Parameter "length" of array elements is incorrect (600, but not 200).
When I change length in PhpMyAdmin to 150 code "says" that length is 
450.
When I change length in PhpMyAdmin to 1 code "says" that length is 3.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-23 07:17 UTC] uw@php.net
Sounds bogus. The mysql extensions in 5.2 forward 1 by 1 what the C API = Mysql returns. The multiplier of 3 hints that a 3-byte charset gets used in one case and a 1-byte charset in the other

Without information on the character sets used, this cannot be verified. You need to provide character set information
 [2009-10-23 07:18 UTC] uw@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

What is needed is all the character sets: MySQL server, client etc.
 [2009-10-23 20:21 UTC] aavolkoff at gmail dot com
MySQL charset: UTF-8 Unicode (utf8)

<?
	$mysqli = new mysqli("localhost", "root", "");
	if (mysqli_connect_errno()) return false;  
	$db_name = "local";
	$query = "CREATE DATABASE  `$db_name`";
	$mysqli->real_query($query);
	$mysqli->select_db($db_name);
	$table_name = "Users";
	$query = "CREATE TABLE `".$table_name."`(id int not null 
auto_increment, name varchar(200), description varchar(200), primary 
key (id)) TYPE=MyISAM";
	$mysqli->real_query($query);
	$query        = "SELECT * FROM `$table_name` LIMIT 0";
	$query_result = $mysqli->real_query($query);
	if ($query_result)
	{
		$mysqli_result = $mysqli->use_result();
		$mysqli_fields = $mysqli_result->fetch_fields();
		$mysqli_result->free_result();
		print_r($mysqli_fields);
	}
?>
 [2009-10-31 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-11-04 20:39 UTC] uw@php.net
Ah, now I get what is puzzling you: 200 -> 600, sorry missed that on the first glance.

Not a bug, length is length in bytes. UTF8 uses 3 bytes per character, 200 * 3 = 600.

From the underlying C API documentation:

 The width of the field. This corresponds to the display length, in bytes.

The server determines the length value before it generates the result set, so this is the minimum length required for a data type capable of holding the largest possible value from the result column, without knowing in advance the actual values that will be produced by the query for the result set. 



 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 01:01:28 2024 UTC