|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29841 mysqli_fetch_field wrong max_length w/ empty query
Submitted: 2004-08-25 18:31 UTC Modified: 2004-08-25 19:32 UTC
From: maillist at pnpitalia dot it Assigned:
Status: Not a bug Package: MySQL related
PHP Version: 5CVS-2004-08-25 (dev) OS: linux gentoo 3q2004
Private report: No CVE-ID: None
 [2004-08-25 18:31 UTC] maillist at pnpitalia dot it
mysqli_fetch_field return an object like this (printed with print_r):
stdClass Object
    [name] => username
    [orgname] => username
    [table] => utenti
    [orgtable] => utenti
    [def] => 
    [max_length] => 0
    [flags] => 16392
    [type] => 253
    [decimals] => 0
the property [max_length] should contain the lenght of the field.

Often empty query (select * from utenti where id = -1) are used to retrieve the description of the query when u want to  automatically generate a form to insert a record, so I think it's important that this function work also with this kind of query.

My workaround for now is to open 2 recordset one selecting the first row of the table (SELECT ... WHERE 1 LIMIT 0,1) and the other with the right where clause.

BTW in php 4.3.x and obviously with the mysql extension not mysqli it work well.

Reproduce code:

// modified mysqli_fetch_field manual example

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());

$query = "SELECT Name, SurfaceArea from Country
where  code = 'ZZZ'

if ($result = mysqli_query($link, $query)) {

    /* Get field information for all fields */
    while ($finfo = mysqli_fetch_field($result)) {

        printf("Name:     %s\n", $finfo->name);
        printf("Table:    %s\n", $finfo->table);
        printf("max. Len: %d\n", $finfo->max_length);
        printf("Flags:    %d\n", $finfo->flags);
        printf("Type:     %d\n\n", $finfo->type);

/* close connection */


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-25 18:57 UTC] maillist at pnpitalia dot it
worst ... almost for varchar (type=253) field it *always* return the length of the string contained in the recordset .
i.e. with the previous example:
if used SELECT username WHERE username ="12345"

[max_length] => 5
 [2004-08-25 19:11 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

mysql(i)_fetch_field returns metadata information for 
resultset, not the tabledefinition. 
Same behaviour for ext/mysql (mysql_fetch_field) 
 [2004-08-25 19:32 UTC] maillist at pnpitalia dot it
sorry, I was looking at a piece of software that has stopped to work after a major LAMP upgrade and I'we not thinked to read it.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Mar 24 18:03:39 2023 UTC