简体   繁体   English

如何在codeigniter中获取mysql表的字段名称?

[英]How to get field names of mysql table in codeigniter?

i am new in codeigniter.我是 codeigniter 的新手。 And i am trying to get field name of a table with a query.我正在尝试通过查询获取表的字段名称。

I have write a query我写了一个查询

"select * from user" “从用户中选择 *”

and pass it to $this->db->query() function.并将其传递给$this->db->query()函数。 i am getting records.我正在获取记录。 But i want to get field names of table.但我想获取表的字段名称。 so how can i get that?那我怎么能得到呢?

Can anybody help me please.任何人都可以请帮助我。 Thanks in advance.提前致谢。

using database library write this code to list all fields:使用数据库库编写此代码以列出所有字段:

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

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

Some Times this may helpful有时这可能会有所帮助

$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;
}

What you did is to get the datas from the table.... here your table is user so你所做的是从表中获取数据......这里你的表是用户所以

in your model function do this...在您的模型函数中执行此操作...

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

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
}

hope this will help you希望对你有帮助

you can use the below code to fetch the field from db您可以使用以下代码从数据库中获取字段

$fields = $this->db->list_fields('table_name');

foreach ($fields as $field)
{
   echo $field;
}

with the help of this code we can fetch the field names from db借助此代码,我们可以从 db 中获取字段名称

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM