php 如何在codeigniter中获取mysql表的字段名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10258128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get field names of mysql table in codeigniter?
提问by Chirayu
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"
“从用户中选择 *”
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.
任何人都可以请帮助我。提前致谢。
回答by semsem
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
回答by Lead Developer
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;
}
回答by Anwar
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
希望能帮到你
回答by Anwar
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;
}
回答by Anwar
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**

