【mysql】获得数据库中表的所有字段名

将数据库的表数据导出到Excel文件中的时候,可能需要用到对应的表字段名,作为Excel表格的每列标题,所以就需要用SQL代码来获取数据库中对应表的所有字段名,然后和数据一一循环显示出来。请看如下代码:

/**
	 * 获得表中所有字段名
	 * Enter description here ...
	 * @param unknown_type $tablename
	 */
	private function get_itemname($tablename){

		$sql = "select COLUMN_NAME as name from information_schema.COLUMNS where table_name = '".$tablename."'";

		$this->load->model ( 'admin_model' );

		$arrdatas = $this->admin_model->find($sql);

		$itemname = array();

		foreach($arrdatas as $data){
			$NameValue = $data->name;
			if($NameValue == "id"){
				continue;
			} 
			$itemname[] = $NameValue;
		}

		return $itemname;
	}

 

Author: jhonse

Jhonse技术博客: http://blog.jhonse.com

Leave a Reply

Your email address will not be published. Required fields are marked *