如何在代码点火器中获取 mysql 表的字段名称


How to get field names of mysql table in codeigniter?

我是codeigniter的新手。我正在尝试通过查询获取表的字段名称。

我写了一个查询

"从用户中选择 *"

并将其传递给$this->db->query()函数。 我正在获得记录。但是我想获取表的字段名称。那么我怎样才能得到呢?

任何人都可以帮我吗?提前谢谢。

使用数据库库编写以下代码以列出所有字段:

$this->db->list_fields('table')

看看这里: https://codeigniter.com/userguide3/database/results.html#CI_DB_result::list_fields

有时这可能会有所帮助

$fields = $this->db->field_data('table_name');
foreach ($fields as $field)
{
   echo $field->name;
   echo $field->type;
   echo $field->max_length;
   echo $field->primary_key;
}

您所做的是从表中获取数据....在这里,您的表是用户,因此

在您的模型函数中执行此操作...

function get_field()
{
$result = $this->db->list_fields('user');
foreach($result as $field)
{
$data[] = $field;
return $data;
}
}

在您的控制器中执行此操作

function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}

在您看来,这样做

foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}

希望这对你有帮助

您可以使用

以下代码从数据库获取字段

$fields = $this->db->list_fields('table_name');
foreach ($fields as $field)
{
   echo $field;
}
借助

此代码,我们可以从数据库中获取字段名称

include('db.php');
    $col="SHOW COLUMNS FROM `camera details`";
    $output=mysqli_query($db,$col);
    $kal=array();
    while($row=mysqli_fetch_array($output))
    {
        if($row['Field']!='id')
        {
            ?><div class="values"><?php echo $row['Field'];
            echo "<br>";?></div><br><br><?php
            array_push($kal, $row['Field']);
        }
    }
    ?>
    <?php** 
The answer given above by Anwar needs modification, there is no need for foreach loop in model function as it will only give the first element despite using foreach, which means the foreach loop is not bringing the fields using $data[], below code should give you 100% result
in your model function do this...
function get_field()
{
$result = $this->db->list_fields('user');
return $result();
}
in your controller do this
function get_field()
{
$data['field'] = $this->model_name->get_field();
$this->load->view('view_name',$data);
}
in your view do this
foreach($field as $f)
{
echo $f."<br>"; //this will echo all your fields
}