How can this be done with the same result using my

2019-09-03 05:44发布

问题:

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:

I have the following code

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;
        }
    }
}

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.

回答1:

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
    }
}

More here on php.net



标签: php mysql mysqli