How to get field names of table in Codeigniter

Created at 24-May-2021 , By samar

How to get field names of table in Codeigniter

In this tutorial, we will try to find the solution to "How to get field names of table in Codeigniter" through programming.

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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.