php.net |  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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: maillist at pnpitalia dot it
New email:
PHP Version: OS:

 

 [2004-08-25 18:31 UTC] maillist at pnpitalia dot it
Description:
------------
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:
---------------
<?php

// 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());
    exit();
}

$query = "SELECT Name, SurfaceArea from Country
where  code = 'ZZZ'
ORDER BY Code LIMIT 5";

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);
    }
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] georg@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC