php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #28912 MYSQLI_TYPE_STRING != mysqli_fetch_field() type for VARCHAR column
Submitted: 2004-06-24 18:50 UTC Modified: 2004-06-29 08:21 UTC
From: danielc at analysisandsolutions dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2004-06-24 (dev) OS: Windows 2000
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: danielc at analysisandsolutions dot com
New email:
PHP Version: OS:

 

 [2004-06-24 18:50 UTC] danielc at analysisandsolutions dot com
Description:
------------
The "type" value returned from mysqli_fetch_field() for a VARCHAR field is 253.  The manual says the constant for VARCHAR fields is MYSQLI_TYPE_STRING which has a value of 254.  So, there's no way to determine column types via constants for VARCHAR's.

Reproduce code:
---------------
mysqli_query($db->connection,
             'CREATE TABLE bar (Cf VARCHAR(5))');

$r = mysqli_query($db->connection,
                  'SELECT Cf FROM bar');
$tmp = mysqli_fetch_field($r);
echo "type found = $tmp->type\n";
echo 'MYSQLI_TYPE_STRING = ' . MYSQLI_TYPE_STRING . "\n";

mysqli_query($db->connection,
             'DROP TABLE bar');


Expected result:
----------------
type found = 254
MYSQLI_TYPE_STRING = 254

Actual result:
--------------
type found = 253
MYSQLI_TYPE_STRING = 254

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-06-28 18:51 UTC] georg@php.net
Looks like a documentation problem. However I couldn't 
find this in documentation. Also the example output in 
mysqli_fetch_field returns 254. 
 
Could you please give me a link to the wrong 
documentation? 
 [2004-06-28 19:35 UTC] danielc at analysisandsolutions dot com
Documentation isn't involved.  This is purely code.  The "type" property returned from the function != the value of the constant.

Perhaps your getting the right result is due to both of us running different versions of the software?  Here's what I'm on:

MySQL:  Ver 14.5 Distrib 4.1.2-alpha, for Win95/Win98 (i32)
PHP:    PHP 5.0.0-dev (cli) (built: Jun 28 2004 16:29:27)
 [2004-06-28 23:48 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

varchar column definition returns MYSQLI_TYPE_VAR_STRING, 
char column definition returns MYSQLI_TYPE_STRING. 
 [2004-06-29 01:37 UTC] danielc at analysisandsolutions dot com
Ah.  Thanks for the clarification.  Then phpdoc/en/reference/mysqli/constants.xml needs updating.

You said:
varchar column definition returns MYSQLI_TYPE_VAR_STRING,
char column definition returns MYSQLI_TYPE_STRING.

Though the docs say:
MYSQLI_TYPE_STRING  (integer) 	Field is defined as VARCHAR
MYSQLI_TYPE_CHAR (integer) 	Field is defined as CHAR

BUT, not so fast...  CHAR columns return 253, but MYSQLI_TYPE_STRING's value is 254.  And MYSQLI_TYPE_CHAR is defined, but what's it for?

<?php
mysqli_query($db->connection,
             'CREATE TABLE bar (Vf VARCHAR(5),'
             . ' Cf CHAR(5))');

$r = mysqli_query($db->connection,
                  'SELECT Vf, Cf FROM bar');

$tmp = mysqli_fetch_field($r);
echo "$tmp->name type found = $tmp->type\n";
echo 'MYSQLI_TYPE_VAR_STRING = ' . MYSQLI_TYPE_VAR_STRING . "\n\n";

$tmp = mysqli_fetch_field($r);
echo "$tmp->name type found = $tmp->type\n";
echo 'MYSQLI_TYPE_STRING = ' . MYSQLI_TYPE_STRING . "\n\n";

echo "So, what's this for?...\n";
echo 'MYSQLI_TYPE_CHAR = ' . MYSQLI_TYPE_CHAR . "\n";

mysqli_query($db->connection,
             'DROP TABLE bar');
?>

vvvvvvv OUTPUT vvvvvvvvvvvv
Vf type found = 253
MYSQLI_TYPE_VAR_STRING = 253

Cf type found = 253
MYSQLI_TYPE_STRING = 254

So, what's this for?...
MYSQLI_TYPE_CHAR = 1
^^^^^^^^^^^^^^^^^^^^^^^^^^^
 [2004-06-29 07:50 UTC] georg@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2004-06-29 08:21 UTC] danielc at analysisandsolutions dot com
If anyone wonders about the second example returning 253/MYSQLI_TYPE_VAR_STRING for the CHAR column, it's due to silent column changes by MySQL:

"if a table contains any variable-length columns... all CHAR columns longer than three characters are changed to VARCHAR columns."

http://dev.mysql.com/doc/mysql/en/Silent_column_changes.html
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 11 07:01:28 2024 UTC