用PHP导出数据到Excel

时间:2018-11-17 18:21:28

标签: php mysql phpexcel

将数据导出到excel时出现错误
警告:mysql_field_name()期望参数1为资源
如何解决?

这是我的代码

include('csql.php');

if(isset($_POST['export_data'])){

    $query5="select * from student";
    $result5 = mysqli_query($mysqli, $query5);

    $filename = "excelfilename";

    header("Content-Type: application/vnd.ms-excel");    
    header("Content-Disposition: attachment; filename=$filename.xls"); 
    header("Accept-Ranges:bytes");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0" );
    header("Pragma: no-cache"); 
    header("Expires: 0");
    $sep = "\t"; 

    if (!$result5) {
        $message  = 'Invalid query: ' . mysql_error() . "\n";
        die($message);
    }

    for ($i = 0;$i < mysqli_num_fields($result5);$i++)  { 
        echo mysql_field_name($result5,$i) . "\t";  
    }  print("\n");
    //print data
}

这是csql.php

$host       =   'localhost';
$user       =   'root';
$password   =   '';
$database   =   'alumniSystem';

$mysqli     =   new mysqli($host,$user,$password,$database)or die("Could not connect to database: " . mysqli_connect_error() . "\n");

mysqli_query($mysqli, "SET CHARACTER SET utf8");

0 个答案:

没有答案