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

时间:2022-10-06 12:37:41

i am new in codeigniter. And i am trying to get field name of a table with a query.

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

I have write a query

我写了一个查询

"select * from user"

“select * from user”

and pass it to $this->db->query() function. i am getting records. But i want to get field names of table. so how can i get that?

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

Can anybody help me please. Thanks in advance.

请有人帮帮我。提前致谢。

5 个解决方案

#1


32  

using database library write this code to list all fields:

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

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

take a look here: http://www.codeigniter.com/user_guide/database/results.html#CI_DB_result::list_fields

看看这里:http://www.codeigniter.com/user_guide/database/results.html#CI_DB_result::list_fields

#2


8  

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

#3


3  

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

希望对你有帮助

#4


0  

you can use the below code to fetch the field from db

您可以使用以下代码从db中获取字段

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

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

#5


0  

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** 

#1


32  

using database library write this code to list all fields:

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

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

take a look here: http://www.codeigniter.com/user_guide/database/results.html#CI_DB_result::list_fields

看看这里:http://www.codeigniter.com/user_guide/database/results.html#CI_DB_result::list_fields

#2


8  

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

#3


3  

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

希望对你有帮助

#4


0  

you can use the below code to fetch the field from db

您可以使用以下代码从db中获取字段

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

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

#5


0  

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**