如何使用mysqli获得相同的结果? [英] How can this be done with the same result using mysqli?
问题描述
我正在尝试将一些代码从mysql_移植到mysqli_等效项,但是我偶然发现了一些似乎不是直接转换的内容:
I am working on porting some code over from mysql_ to mysqli_ equivalents, however I stumbled accross something that doesn't seem to be a straight conversion:
我有以下代码
if($field_flags == false) {
for($j=0; $j<$num_fields; $j++) {
$field_flags[$j] = mysql_field_flags($result, $j);
if(strpos($field_flags[$j], 'not_null') !== false) {
$field_flags[$j] = false;
} else {
$field_flags[$j] = true;
}
}
}
由于新方法"mysqli_fetch_field_direct"似乎不太简单,我无法移植它.
And I am having trouble porting it as the new method "mysqli_fetch_field_direct" doesn't seem to be as straightforward.
有人可以协助我移植上面的代码.本质上,我正在检查该字段是否允许空值.
Could someone please assist me with porting the above code. Essentially, I am checking if the field allows nulls or not.
推荐答案
函数mysqli_fetch_field_direct
返回object
,而mysql_field_flags
则返回string
.因此,直接使用strpos
无效.
The function mysqli_fetch_field_direct
returns an object
, where as mysql_field_flags
retuns a string
. Hence your direct use of strpos
won't work.
if($field_flags == false) {
for($j=0; $j<$num_fields; $j++) {
$field_flags[$j] = mysqli_fetch_field_direct($result, $j);
if(strpos($field_flags[$j]->flags, 'NOT_NULL') !== false) {
//Do something here
} else {
//Do something else
}
}
更多信息,请参见 php.net
这篇关于如何使用mysqli获得相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!