首页 > 解决方案 > 如何使用 php 从 mysql 导出数据?

问题描述

我使用 numberic 将我的休假状态存储在 mysql 数据库中。如果编号为 1 则批准请假,如果编号为 2 则请假已被拒绝。现在我现在面临的问题是我不知道如何使用 php 将数字转换为字符串。我希望在我导出的 .csv 文件中获得批准和拒绝这个词。现在它只显示数字 1 和 2 。

$query ="SELECT tblemployees.FirstName,tblemployees.LastName,tblemployees.EmpId,tblemployees.Gender,tblemployees.Phonenumber,tblemployees.EmailId,applyleave.id,applyleave.LeaveType,applyleave.FromDate,applyleave.duration,applyleave.ToDate,applyleave.descr,applyleave.PostingDate,applyleave.sta,applyleave.adminRemark,applyleave.AdminRemarkDate from applyleave inner join tblemployees on applyleave.employeeID=tblemployees.EmpId";
 $result = mysqli_query($db, $query);
 if(mysqli_num_rows($result) > 0)
 {

  $output .= '
   <table class="table" bordered="1">  
                    <tr>  
                        <th>No</th>  
                        <th>Employee ID</th>  
                        <th>Employee Name</th>  
                        <th>Employee Email</th>
                        <th>Employee Contact Number</th>
                        <th>Leave Type</th>  
                        <th>From Date</th>  
                        <th>To Date</th>
                        <th>Duration</th>
                        <th>Reason</th>  
                        <th>Leave Status</th>  
                        <th>Request Date</th>  
                        <th>Admin Remark</th>
                        <th>Admin Remark Date</th>

                    </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $sta='.$row["sta"].';
    if($sta==1){
        $status="Approved";

    }
    if($sta==2){
        $status="Rejected";
    }
   $output .= '

                    <tr>  
                        <td>'.$row["id"].'</td>
                        <td>'.$row["EmpId"].'</td>  
                        <td>'.$row["LastName"].' '.$row["FirstName"].'</td>  
                        <td>'.$row["EmailId"].'</td>  
                        <td>'.$row["Phonenumber"].'</td>  
                        <td>'.$row["LeaveType"].'</td>  
                        <td>'.$row["FromDate"].'</td> 
                        <td>'.$row["ToDate"].'</td>  
                        <td>'.$row["duration"].'</td>
                        <td>'.$row["descr"].'</td>  
                        <td>'.$status.'</td>
                        <td>'.$row["PostingDate"].'</td>  
                        <td>'.$row["adminRemark"].'</td> 
                        <td>'.$row["AdminRemarkDate"].'</td>  

                     </tr>
   ';
  }
$output .= '</table>';
  header('Content-Type: application/xls');
  header('Content-Disposition: attachment; filename='. date('Y-m-d') .' Leave Balance.xls');
  echo $output;

标签: phpmysqlexport

解决方案


您的错误是您要附加点.。到变量并使用单引号。单引号上的变量不会被解释并且它们的值不会被返回。

//Your code
$sta='.$row["sta"].'; // This will save on $sta ( .$row["sta"]. ) because you are using single quotes.

// Update to
$sta=$row["sta"];  // you don't need quotes or dots in the variable


还有 if 语句,我会将其更改为:

if($sta==1){
    $status="Approved";
}else if($sta==2){
    $status="Rejected";
}else{
    $status = "Not Defined";
}

考虑其他值,如空值等。

PHP 参考


推荐阅读