php 反序列化MYSQL数据并导出为CSV [已关闭]

o7jaxewo  于 2023-03-16  发布在  PHP
关注(0)|答案(1)|浏览(133)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
3天前关闭。
Improve this question
好的,我有MYSQL数据,我可以完美地将其导出到CSV。这里的问题是其中一列有序列化的数据,当我导出时,它显示为序列化。我尝试过PHP的未消毒功能,但它似乎不工作。
请查找以下代码以导出到CSV,并且具有序列化数据的列为fine_type。
谢谢

导出到CSV的代码

<?php 
// Load the database configuration file 
include 'connect.php';
//$h=$_REQUEST['inspector_name'];
//$emp_numberd=$_REQUEST['datepicker'];

// Filter the excel data 
function filterData(&$str){ 
    $str = preg_replace("/\t/", "\\t", $str); 
    $str = preg_replace("/\r?\n/", "\\n", $str); 
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
} 
 
// Excel file name for download 
$fileName = "members-data_" . date('d-m-Y') . ".xls"; 
 
// Column names 
$fields = array('اسم المحل','.الرخصة','اسم المفتش','Fine Date','Fine Details','المستفيد الحقيقي'); 
 
// Display column names as first row 
$excelData = implode("\t", array_values($fields)) . "\n"; 
 
// Fetch records from database 
$query = $link->query("SELECT  * FROM fine_controls WHERE str_to_date(fine_date, '%d-%m-%Y') between str_to_date('01-11-2022', '%d-%m-%Y') and str_to_date('10-03-2023', '%d-%m-%Y') order by id desc"); 

if($query->num_rows > 0){ 
    // Output each row of the data 
    while($row = $query->fetch_assoc()){
        
        $status = ($row['statuse'] == 1)?'Active':'Inactive'; 
        $lineData = array($row['shopname'],$row['license'],$row['inspector_name'],$row['fine_date'],$row['fine_type'],$row['beneficiary']); 
        array_walk($lineData, 'filterData'); 
        $excelData .= implode("\t", array_values($lineData)) . "\n"; 
    } 
}else{ 
    $excelData .= 'No records found...'. "\n"; 
} 
 
// Headers for download 
header("Content-Type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=\"$fileName\""); 
 print chr(255) . chr(254).mb_convert_encoding($excelData, 'UTF-16LE', 'UTF-8');
// Render excel data 
//echo $excelData; 
 
exit;

已尝试的更改

if($query->num_rows > 0){ 
        // Output each row of the data 
        while($row = $query->fetch_assoc()){
 $data=unserialize($row['fine_type']);
            
            $status = ($row['statuse'] == 1)?'Active':'Inactive'; 
            $lineData = array($row['shopname'],$row['license'],$row['inspector_name'],$data,$row['fine_type'],$row['beneficiary']); 
            array_walk($lineData, 'filterData'); 
            $excelData .= implode("\t", array_values($lineData)) . "\n"; 
        } 
    }else{ 
        $excelData .= 'No records found...'. "\n"; 
    }
krcsximq

krcsximq1#

由于序列化列$row['fine_type']是一个字符串数组,因此您必须做一些事情来科普它。我建议在输出中为每次出现的描述fine的字符串创建一个新行,如下所示。
这当然取决于用户对该输出的要求。

while($row = $query->fetch_assoc()){           
            $status = ($row['statuse'] == 1)?'Active':'Inactive';             
            
            // $row['fine_type'] is an serialized array of strings
            $data=unserialize($row['fine_type']);
            
            // so loop round it and create a line per occurance
            foreach($data as $fine) {
                $lineData = [ 
                              $row['shopname'], $row['license'], $row['inspector_name'], 
                              $fine,  $row['beneficiary']
                            ];
                array_walk($lineData, 'filterData'); 
                $excelData .= implode("\t", array_values($lineData)) . "\n"; 
            }
        }

相关问题