使用 PHPExcel 获取从 MySQL 到 Excel 的字段名称

Getting field name from MySQL to Excel using PHPExcel

我正在尝试使用 PHPExcel 从我的数据库中获取 table 的字段名称。

但是,我收到一条错误消息

mysql_field_name() expects parameter 2 to be long, string given in C:\xampp\htdocs\phpexcelsample\download.php on line 42

我想我在这里遗漏了一些东西...你能给我一些关于如何获取字段名的提示吗?

这是我的代码:

<?php




$dbhost= "localhost"; //your MySQL Server 
$dbuser = "root"; //your MySQL User Name 
$dbpass = ""; //your MySQL Password 
 $dbname = "sales"; 
//your MySQL Database Name of which database to use this 
 $tablename = "deposit"; 
//your MySQL Table Name which one you have to create excel file 
 // your mysql query here , we can edit this for your requirement 
 $sql = "Select * from $tablename "; 
//create  code for connecting to mysql 
$connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . 
mysql_errno()); 
//select database 
$Db = @mysql_select_db($dbname, $connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . 
mysql_errno()); 
//execute query 
$result = @mysql_query($sql,$connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . 
mysql_errno()); 

error_reporting(E_ALL);


require_once 'Classes/PHPExcel.php';
// Execute the database query
// Instantiate a new PHPExcel object 
$objPHPExcel = new PHPExcel();  
// Set the active Excel worksheet to sheet 0 
$objPHPExcel->setActiveSheetIndex(0);  
// Initialise the Excel row number 
$rowCount = 1;  

//start of printing column names as names of MySQL fields  
$column = 'A';
for ($i = 1; $i < mysql_num_fields($result); $i++)    
{
//$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, 
mysql_field_name($result,$i));
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, 
mysql_field_name($result,'EMPLOYEE'));
$column++;
}
//end of adding column names  

//start while loop to get data  
$rowCount = 2;  
while($row = mysql_fetch_row($result))  
{  
$column = 'A';
for($j=1; $j<mysql_num_fields($result);$j++)  
{  
    if(!isset($row[$j]))  
        $value = NULL;  
    elseif ($row[$j] != "")  
        $value = strip_tags($row[$j]);  
    else  
        $value = "";  

    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
    $column++;
}  
$rowCount++;
} 


// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="Result.xls"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output');









?>

你的错误 mysql_field_name($result,'EMPLOYEE'));,因为 mysql_field_name 第二个参数需要整数 http://php.net/manual/en/function.mysql-field-name.php

如果您想显示特定的字段名称,那么您可以在数组中获取它并使用它。例如:

<?php
$query = 'SHOW COLUMNS FROM `table`';
$fields = [];
try {
    /* $mysqli connection object */

    if(!$res = $mysqli->query($query)) {
        throw new Exception($mysqli->error);
    }

    while($row = $res->fetch_assoc()) {
        $fields[] = $row['Field'];
    }
} catch(Exception $e) {
    echo $e->getMessage();
}

var_dump($fields);

并查看 http://php.net/manual/en/book.mysqli.php

我已经知道怎么做了:)

这是我的代码:

$dbhost= "localhost"; //your MySQL Server 
$dbuser = "root"; //your MySQL User Name 
$dbpass = ""; //your MySQL Password 
$dbname = "sales"; 
//your MySQL Database Name of which database to use this 
$tablename = "deposit"; 
//your MySQL Table Name which one you have to create excel file 
// your mysql query here , we can edit this for your requirement 
$sql = "Select * from $tablename "; 
//create  code for connecting to mysql 
 $connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . 
mysql_errno()); 
//select database 
$Db = @mysql_select_db($dbname, $connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . 
mysql_errno()); 
 //execute query 
$result = @mysql_query($sql,$connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . 
mysql_errno()); 

error_reporting(E_ALL);

 require_once 'Classes/PHPExcel.php';
 // Execute the database query
// Instantiate a new PHPExcel object 
$objPHPExcel = new PHPExcel();  
// Set the active Excel worksheet to sheet 0 
$objPHPExcel->setActiveSheetIndex(0);  
// Initialise the Excel row number 
$rowCount = 1;  

 while($row = mysql_fetch_array($result)){
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['DATE']);

 $rowCount++;
}