|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 20:00:01 2025 UTC |
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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^