Skip to main content

How to get field names of table in Codeigniter

You can get field (column) names of a table in Codeigniter using the getFieldNames() method which is used in Codeigniter 4. This method returns all the column names of the specified table in array form. You can also use the list_fields() method on the $this->db object to get the list of all columns of the table in codeigniter 3.

  • Get column names of table in codeigniter 4

    --PATH app\Controllers\<Home>.php
    public function getColumnNamesFromTable(){
        $db = \Config\Database::connect();
        $fields = $db->getFieldNames('heroes');
        foreach ($fields as $field)
        {
           echo $field . "<br>";
        }
        die();
    }
    
  • Get column names of table in Codeigniter 3

    $fields = $this->db->list_fields('heroes');
    foreach ($fields as $field)
    {
        echo $field . "<br/>";
    }
    
  • Back to code snippet queries related codeIgniter