问题描述
使用mysqli,我可以得到如下字段的信息:
Using mysqli, I can get information about fields like so
$field = mysqli_fetch_field_direct($result, $fieldCount);
,我可以使用
$field->flags
PHP手册说,这返回表示字段的位标志的整数。但这是所有的信息,我可以找到。如何解释位标志?到目前为止,我已经确定
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
整数(字段长度无所谓)根据指定的属性返回以下位标志:
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)
感谢您提供任何帮助!
推荐答案
请参阅
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
上面公布的每个数是2的幂。(1 = 2 ^ 0,2 = 2 ^ 1,4 = 2 ^ 2等等)。换句话说,它们中的每一个对应于数字中的一个比特。要读取 49967
意味着您可以例如以二进制形式显示
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
>> 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
其他方式检查specific flag is using binary conjunction operator &
and mysqli constants as provided by nickb in below below:
Other way to check, for specific flag is using binary conjunction operator &
and mysqli constants as provided by nickb in comment below:
>> 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:
if($fieldFlags & MYSQLI_PRI_KEY_FLAG) {
echo 'this field is a primary key';
}
这篇关于mysqli中的位标志是什么意思使用fetch_field_direct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!