如果db列可以为NULL设置所需的输入?

is there a way how can I ask if the chosen column can't be null that php should add value "required" (html5) to the input?

Trying to outline the idea:

$vys = mysqli_query($db, "some query here"); 
while($arr = mysqli_fetch_assoc($vys)){
    echo "<input type='text' name='".$arr['name of column']."' value='' ".(canBeNull($arr['name of column']) ? "require" : "")." />";
}

After this, some 'INSERT VALUES(...) INTO table' would follow.. This example is supposed to 'echo' all columns from table so user can fill them up. And yes, I know, that the only query which can be processed with mysqli_fetch_assoc is 'SELECT * FROM column', but I was thinking that this would be very helpful for registration forms, where when registering new user, name and surname is mandatory (NOT NULL column), but nickname is optional (NULL column). And when there are tens of input fields that should be very time-saving.

Don't you please know, how this stuff can be done?

You can add a SHOW COLUMNS FROM table query before hand to get the column list. Then there is a Null column that is either set to Yes or No.

if you execute a query like:

$q = $this->prepare("SHOW COLUMNS FROM `$table`");
$q->execute();

it returns a array like:

Array
(
[0] => Array
    (
        [Field] => dataid
        [0] => dataid
        [Type] => int(11)
        [1] => int(11)
        [Null] => NO
        [2] => NO
        [Key] => PRI
        [3] => PRI
        [Default] => 
        [4] => 
        [Extra] => auto_increment
        [5] => auto_increment
    )

[1] => Array
    (
        [Field] => text
        [0] => text
        [Type] => varchar(255)
        [1] => varchar(255)
        [Null] => NO
        [2] => NO
        [Key] => 
        [3] => 
        [Default] => 
        [4] => 
        [Extra] => 
        [5] => 
    )

)

You could check it like that. Seems like this should be baked into the business logic of the php though and not have to check with the db maybe a attribute in the class the sets $notNull to true in the construct

Mysqli has extra method to get metadata from queries. mysqli_stmt::result_metadata

Example

$stmt = $mysqli->prepare("SELECT id, name FROM friends");
$stmt->execute();

/* get resultset for metadata */
$result = $stmt->result_metadata();

/* retrieve field information from metadata result set */
$fields = $result->fetch_fields(); // array of std objects

And check what field could be null.

(!($field->flags & MYSQLI_NOT_NULL_FLAG))
    echo $field->name;

Other flags could be obtained here: Predefined Constants