Using mysqli, I can get information about fields like so
使用mysqli,我可以获得有关字段的信息
$field = mysqli_fetch_field_direct($result, $fieldCount);
and I can get the field flags from the result using
我可以使用结果从结果中获取字段标志
$field->flags
The PHP manual says that this returns "An integer representing the bit-flags for the field." but that's all the info I can find. How can I interpret the bit flags? So far, I've worked out that
PHP手册说它返回“表示字段位标志的整数”。但这就是我能找到的所有信息。我怎样才能解释位标志?到目前为止,我已经解决了这个问题
Integers (length of field doesn't matter) return the following bit flags depending on the attributes specified:
整数(字段长度无关紧要)根据指定的属性返回以下位标志:
primary key 49967
primary & unique 53255
unique key 53251
foreign key 53257
unique & index 53259 (Auto increment 49675)
Thanks for any help you can offer!
谢谢你尽你所能的帮助!
1 个解决方案
#1
4
See comment at http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828
请参阅http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828上的评论
NOT_NULL_FLAG = 1
PRI_KEY_FLAG = 2
UNIQUE_KEY_FLAG = 4
MULTIPLE_KEY_FLAG = 8
BLOB_FLAG = 16
UNSIGNED_FLAG = 32
ZEROFILL_FLAG = 64
BINARY_FLAG = 128
ENUM_FLAG = 256
AUTO_INCREMENT_FLAG = 512
TIMESTAMP_FLAG = 1024
SET_FLAG = 2048
PART_KEY_FLAG = 16384
GROUP_FLAG = 32768
NUM_FLAG = 32768
UNIQUE_FLAG = 65536
Notice that every number posted above is a power of 2. (1 = 2^0, 2 = 2^1, 4 = 2^2 and so on). In other words, each of them corresponds to one bit in a number. To read what 49967
means, you can for example display it in binary form
请注意,上面张贴的每个数字都是2的幂。(1 = 2 ^ 0,2 = 2 ^ 1,4 = 2 ^ 2,依此类推)。换句话说,它们中的每一个对应于数字中的一位。要阅读49967的含义,您可以以二进制形式显示它
>> decbin(49967);
'1100001100101111'
Starting from right, you can now read that the field has following flags
从右边开始,您现在可以读取该字段具有以下标志
NOT_NULL
PRI_KEY
UNIQUE_KEY
MULTIPLE_KEY
UNSIGNED
ENUM
AUTO_INCREMENT
GROUP
UNIQUE
Other way to check, for specific flag is using binary conjunction operator &
and mysqli constants as provided by nickb in comment below:
其他检查方式,特定标志使用二进制连接运算符&和mysqli常量,如下面的注释中的nickb所提供的:
>> echo MYSQLI_NOT_NULL_FLAG & 49967
1
>> echo MYSQLI_PRI_KEY_FLAG & 49967
2
>> echo MYSQLI_UNIQUE_KEY_FLAG & 49967
4
>> echo MYSQLI_MULTIPLE_KEY_FLAG & 49967
8
>> echo MYSQLI_BLOB_FLAG & 49967
0
Basically you gat non-zero value for flags that are set, and 0 for flags that are unset. You can safely use it in conditions like this:
基本上,您为已设置的标志gat非零值,对于未设置的标志为0。您可以在以下条件下安全地使用它:
if($fieldFlags & MYSQLI_PRI_KEY_FLAG) {
echo 'this field is a primary key';
}
#1
4
See comment at http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828
请参阅http://www.php.net/manual/en/mysqli-result.fetch-fields.php#101828上的评论
NOT_NULL_FLAG = 1
PRI_KEY_FLAG = 2
UNIQUE_KEY_FLAG = 4
MULTIPLE_KEY_FLAG = 8
BLOB_FLAG = 16
UNSIGNED_FLAG = 32
ZEROFILL_FLAG = 64
BINARY_FLAG = 128
ENUM_FLAG = 256
AUTO_INCREMENT_FLAG = 512
TIMESTAMP_FLAG = 1024
SET_FLAG = 2048
PART_KEY_FLAG = 16384
GROUP_FLAG = 32768
NUM_FLAG = 32768
UNIQUE_FLAG = 65536
Notice that every number posted above is a power of 2. (1 = 2^0, 2 = 2^1, 4 = 2^2 and so on). In other words, each of them corresponds to one bit in a number. To read what 49967
means, you can for example display it in binary form
请注意,上面张贴的每个数字都是2的幂。(1 = 2 ^ 0,2 = 2 ^ 1,4 = 2 ^ 2,依此类推)。换句话说,它们中的每一个对应于数字中的一位。要阅读49967的含义,您可以以二进制形式显示它
>> decbin(49967);
'1100001100101111'
Starting from right, you can now read that the field has following flags
从右边开始,您现在可以读取该字段具有以下标志
NOT_NULL
PRI_KEY
UNIQUE_KEY
MULTIPLE_KEY
UNSIGNED
ENUM
AUTO_INCREMENT
GROUP
UNIQUE
Other way to check, for specific flag is using binary conjunction operator &
and mysqli constants as provided by nickb in comment below:
其他检查方式,特定标志使用二进制连接运算符&和mysqli常量,如下面的注释中的nickb所提供的:
>> echo MYSQLI_NOT_NULL_FLAG & 49967
1
>> echo MYSQLI_PRI_KEY_FLAG & 49967
2
>> echo MYSQLI_UNIQUE_KEY_FLAG & 49967
4
>> echo MYSQLI_MULTIPLE_KEY_FLAG & 49967
8
>> echo MYSQLI_BLOB_FLAG & 49967
0
Basically you gat non-zero value for flags that are set, and 0 for flags that are unset. You can safely use it in conditions like this:
基本上,您为已设置的标志gat非零值,对于未设置的标志为0。您可以在以下条件下安全地使用它:
if($fieldFlags & MYSQLI_PRI_KEY_FLAG) {
echo 'this field is a primary key';
}