数据未通过 PHP 从 MySQL 完全导出到 CSV

Data is not being fully exported from MySQL to CSV via PHP

上下文:

我正在使用以下堆栈:HTML/CSS、PHP、MySQL 和 XAMPP 来构建一个网络应用程序来存储和检索输入到一个表格。

问题:

我有两个文件 (i) export.php 和 (ii) export_index.php。从下图中可以看出,export_index.php 文件对数据进行了索引,当在网络上本地查看时,它是我的 MySQL 数据库中数据的完整表示浏览器。 export.php 文件应该输出到本地下载文件夹,与您在 export_index.php 文件中看到的完全相同到 .csv 格式的文件,以便在 Excel 中轻松打开。

阅读文档并尝试调试后,我仍然无法找出错误所在,但我认为它来自 $data[] 的 either/both 和 $row .感谢我能就此错误获得的任何帮助。感谢您的宝贵时间!

图片:

index_export.php 文件的输出

export.php 文件的输出

代码:

index_export.php

<?php
ob_start();

/*
* this file is used to index the data stored in 
* the MySQL database. it will display the data
* on the screen that is in the db but DOESNT
* format the data for excel files. export.php
* does that.
*/


// dn login credentials
include("dbconfig.php");


// connect with credentials held in dbconfig file
$con = new mysqli($server, $user, $pass, $db);


if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
} 


session_start();



// List Data
$query = "SELECT * FROM comp";
if (!$result = mysqli_query($con, $query)) {
    exit(mysqli_error($con));
}


if (mysqli_num_rows($result) > 0) {
    $data = '<table class="table table-bordered">
        <tr>
            <th>Title</th>
            <th>Overview</th>
            <th>Threat Details</th>
            <th>Customer Name</th>
            <th>Field Insight</th>
            <th>Competitor</th>
            <th>ID</th>
            <th>Revenue Damage</th>
            <th>Technology</th>
            <th>Geography</th>
        </tr>
    ';
    while ($row = mysqli_fetch_assoc($result)) {
        $data .= '<tr>
        <td>'.$row['titleBinded'].'</td>
        <td>'.$row['overviewBinded'].'</td>
            <td>'.$row['threatDetailsBinded'].'</td>
            <td>'.$row['customerNameBinded'].'</td>
            <td>'.$row['fieldInsightBinded'].'</td>
            <td>'.$row['competitorBinded'].'</td>
            <td>'.$row['idBinded'].'</td>
            <td>'.$row['revDamageBinded'].'</td>
            <td>'.$row['techBinded'].'</td>  
            <td>'.$row['geoBinded'].'</td>
        </tr>';
    }
    $data .= '</table>';
}

?>



<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Export Database Data</title>
    <!-- Bootstrap CSS File  -->
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css"/>
</head>
<body>
<div class="container">
    <!--  Header  -->
    <div class="row">
        <div class="col-md-12">
            <h2>Export Data from MySQL to CSV</h2>
        </div>
    </div>
    <!--  /Header  -->

    <!--  Content   -->
    <div class="form-group">
        <?php echo $data ?>
    </div>
    <div class="form-group">
        <button onclick="Export()" class="btn btn-primary">Export to CSV File</button>
    </div>
    <!--  /Content   -->

    <script>
        function Export()
        {
            var conf = confirm("Export users to CSV?");
            if(conf == true)
            {
                window.open("export.php", '_blank');
            }
        }
    </script>
</div>
</body>
</html>

export.php

<?php
ob_start();


// passes in the login credentials
include("dbconfig.php");

// !!!CAUSED A MEMORY LEAK!!!!
// includes the index file
//include("export.php");


// connect with credentials held in dbconfig file
$con = new mysqli($server, $user, $pass, $db);
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}


session_start();   


// list all data in the db
$query = "SELECT * FROM comp";
if (!$result = mysqli_query($con, $query)) {
     exit(mysqli_error($con));
}


$data = array();
if (mysqli_num_rows($result) > 0) {

    while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
}


// header labels for the CSV file
header('Content-Type: text/csv; charset=utf-8');

header('Content-Disposition: attachment; filename = comp.csv');

$output = fopen('php://output', 'w');

fputcsv($output, array('Title', 'Overview', 'Threat Details', 'Customer Name', 'Field Insight', 'Competitor', 'ID', 'Revenue Damage', 'Technology', 'Geography') );



// iteratively exports row by row
if (count($comp) > 0) {
    foreach ($data as $row) {
    fputcsv($output, $row);
    }
}

?>

create.sql

drop database if exists comp;
create schema comp;
use comp;

create table comp (
    titleBinded VARCHAR(50),
    overviewBinded VARCHAR(255),
    threatDetailsBinded VARCHAR(255),
    customerNameBinded VARCHAR(30),
    fieldInsightBinded VARCHAR(255),
    competitorBinded VARCHAR(30),
    idBinded INT,
    revDamageBinded INT,
    techBinded VARCHAR(225),
    geoBinded VARCHAR(255),
    PRIMARY KEY (idBinded)
);

您在 export.php 文件中犯了一个小错误:

// iteratively exports row by row
 if (count($comp) > 0) {
   foreach ($data as $row) {
      fputcsv($output, $row);
   }
 }

在上面代码的if条件中使用

if(count($data) > 0)

而不是

 if(count($comp) > 0)

祝你好运!!!