dongzongzhi6953 2016-06-15 02:56
浏览 71

使用php PDO从数据库导出到csv文件?

i have here the code for exporting to csv file from my database using pdo.The downloading of the file works fine and it opens normally but before it opens something just always pop up every time i export it to csv file. what i want is to get rid of it.

this is the pop up message

->the file that you are trying to open is in a different format than sprecified by the file extension. verify that the file is not corrupted and is from a trusted source before opening the file.

->excel has detected that .csv is a SYLK file, but cannot load it.Either the file has errors or is is not a SYLK file format..

user.class.php

public function export($query)
{
$dsn = 'mysql:host=localhost;dbname=survey';
$username = 'root';
$password = '';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);

try {
$db = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
$error_message = $e->getMessage();

}

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=REGION V-Survey.csv");
header("Pragma: no-cache");
header("Expires: 0");


try {
   $statement = $db->prepare($query);
   $statement->execute();
   $results = $statement->fetchAll();
   $statement->closeCursor();

   $content = '';
   $title = '';
   foreach ($results as $rs){
       $content .= stripslashes($rs["ID"]). ',';
       $content .= stripslashes($rs["Province"]). ',';
       $content .= stripslashes($rs["Date_Submitted"]). ',';
       $content .= stripslashes($rs["Survey_Number"]). ',';
       $content .= stripslashes($rs["Land_Type"]). ',';
       $content .= stripslashes($rs["C_Size"]). ',';
       $content .= stripslashes($rs["Contractor"]). ',';
       $content .= stripslashes($rs["Module_Number"]). ',';
       $content .= stripslashes($rs["LAD_Target"]). ',';
       $content .= stripslashes($rs["Fund_Year"]). ',';
       $content .= stripslashes($rs["Land_Cat"]). ',';
       $content .= stripslashes($rs["Title_Number"]). ',';
       $content .= stripslashes($rs["W_bal"]). ',';
       $content .= stripslashes($rs["Lot_Number"]). ',';
       $content .= stripslashes($rs["Proj_Number"]). ',';
       $content .= stripslashes($rs["Agency"]). ',';
       $content .= stripslashes($rs["Location"]). ',';
       $content .= stripslashes($rs["Barangay"]). ',';
       $content .= stripslashes($rs["Date_Survey"]). ',';
       $content .= stripslashes($rs["No_Lots"]). ',';
       $content .= stripslashes($rs["Area"]). ',';
       $content .= stripslashes($rs["LO_Name"]). ',';
       $content .= stripslashes($rs["Times_Returned"]). ',';
       $content .= stripslashes($rs["Times_Resubmitted"]). ',';
       $content .= stripslashes($rs["Date_Returned"]). ',';
       $content .= stripslashes($rs["Date_Resubmitted"]). ',';
       $content .= stripslashes($rs["Date_Suspended"]). ',';
       $content .= stripslashes($rs["Date_Completed"]). ',';
       $content .= stripslashes($rs["Station"]). ',';
       $content .= stripslashes($rs["Date_Approved"]). ',';
       $content .= stripslashes($rs["Date_DAR"]). ',';
       $content .= stripslashes($rs["Date_DARPO"]). ',';
       $content .= stripslashes($rs["Cert"]). ',';
       $content .= stripslashes($rs["Remarks"]). ',';
       $content .= stripslashes($rs["Date_Reported"]). ',';
       $content .= "
";
    }
    $title .= "ID,Province,Date_Submitted,Survey_Number,Land_Type,C_Size,Contractor,Module_Number,LAD_Target,Fund_Year,Land_Cat,Title_Number,W_bal,Lot_Number,Proj_Number,Agency,Location,Barangay,Date_Survey,No_Lots,Area,LO_Name,Times_Returned,Times_Resubmitted,Date_Returned,Date_Resubmitted,Date_Suspended,Date_Completed,Station,Date_Approved,Date_DAR,Date_DARPO,Cert,Remarks,Date_Reported"."
";
    echo $title;
    echo $content;

} catch (PDOException $e) {
      echo $e->getMessage();
   exit;
}
}

export.php

$query = "SELECT * FROM survey_section";
$crud->export($query);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)