Repository navigation
Adding to the field_data function for MySQL/MariaDB #6329
Description
Feature Request: Add is_nullable to field_data() output (MySQLi driver)
Title: Add is_nullable property to field_data() results in the MySQLi driver
Summary
$this->db->field_data($table) currently returns objects with name, type, max_length, primary_key, and default — but no information about whether a column is nullable (NOT NULL vs NULL). This makes it impossible to build generic, metadata-driven forms that correctly mark required vs. optional fields without a second, manual SHOW COLUMNS / information_schema query.
Motivation
We built a generic CRUD form renderer that iterates over field_data($table) to render each column's label and input, and needs to know per-column nullability to:
mark required fields in the UI (asterisk / required attribute)
decide default value / validation behavior
Since field_data() already runs SHOW COLUMNS FROM
Current behavior
system/database/drivers/mysqli/mysqli_driver.php::field_data() builds each $retval[$i] from the SHOW COLUMNS result but stops at primary_key:
$retval[$i]->default = $query[$i]->Default;
$retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
system/database/drivers/mysqli/mysqli_result.php::field_data() builds each $retval[$i] from mysqli_result::fetch_fields() and also stops before using the native is_nullable flag that MySQLi already provides:
$retval[$i]->default = $field_data[$i]->def;
Proposed change
Add one line to each of the two functions above:
mysqli_driver.php (field_data($table), based on SHOW COLUMNS):
$retval[$i]->default = $query[$i]->Default;
$retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI');
$retval[$i]->is_nullable = (int) ($query[$i]->Null === 'YES');
mysqli_result.php (field_data(), based on mysqli_result::fetch_fields()):
$retval[$i]->default = $field_data[$i]->def;
$retval[$i]->is_nullable = $field_data[$i]->is_nullable;
Backward compatibility
Purely additive — adds a new property to the returned stdClass objects, does not change or remove any existing property, and does not alter the method signature. Existing code that ignores the new property is unaffected.
Scope note
This report covers the MySQLi driver (the one we use in production). The same gap likely exists in other drivers with a field_data() implementation (e.g. the legacy mysql driver) — happy to provide the equivalent one-liner for those too if maintainers want consistency across drivers, but MySQLi is the one actively used by most CodeIgniter 3 installs today.
Activity
abhikashbabu commentedon Oct 22, 2026
Hi, this is a great feature request. I can quickly add these lines and submit a PR. Could you please assign this to me?
NielBuys commentedon Oct 26, 2026
This is implemented in my fork in NielBuys/CodeIgniter#22 (same request as #6335). It adds is_nullable to field_data() for mysqli and every other driver, and it will be merged before the fork's next release (3.1.25).
If you'd like to help, please test it and let me know whether it works correctly for you.
One note for anyone implementing the issue's proposal as written: the objects that mysqli_result::fetch_fields() returns have no is_nullable property. The PR reads it from the flags instead, the same way primary_key is already read from MYSQLI_PRI_KEY_FLAG: (int) ! ($field_data[$i]->flags & MYSQLI_NOT_NULL_FLAG).
abhikashbabu commentedon Oct 26, 2026
Hi @NielBuys,
I tested the tasks/issue-6335-is-nullable branch locally with PHP 8.x and MySQLi driver.
Running field_data() on both nullable and non-nullable fields correctly returned the expected values:
Non-nullable columns returned [is_nullable] => 0
Nullable columns returned [is_nullable] => 1
The bitwise flag implementation (int) ! ($field_data[$i]->flags & MYSQLI_NOT_NULL_FLAG) works as expected. Thanks!
NielBuys commentedon Oct 27, 2026
Hi @abhikashbabu, Thanks i merged it. I will do a release in a week or so.