11

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

我写了一个查询

“从用户中选择 *”

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

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

4

6 回答 6

39

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

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

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

于 2012-04-21T10:33:22.820 回答
10

有时这可能会有所帮助

$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;
}
于 2014-06-27T13:24:45.283 回答
4

你所做的是从表中获取数据......这里你的表是用户所以

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

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
}

希望对你有帮助

于 2012-04-21T10:45:36.693 回答
0

您可以使用以下代码从数据库中获取字段

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

foreach ($fields as $field)
{
   echo $field;
}
于 2018-01-10T03:51:58.983 回答
0

借助此代码,我们可以从 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** 
于 2018-01-19T06:59:38.080 回答
0
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
}
于 2021-04-07T14:05:19.530 回答