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

标签 php mysql xampp export-to-csv fgetcsv

上下文:

我正在使用以下堆栈:HTML/CSS、PHP、MySQL 和 XAMPP 来构建一个 Web 应用程序,用于存储和检索输入表单的数据。

问题:

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

阅读文档并尝试调试它后,我仍然无法弄清楚错误在哪里,但我认为它来自 $data[] 中的一个/两者$行。感谢我能在这个错误上得到的任何帮助。感谢您的宝贵时间!

图片:

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)

祝你好运!!!

关于php - 数据未通过 PHP 完全从 MySQL 导出到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51530862/

相关文章:

php - PHP 数组中的键未按数字排序

php - 如何在 PHP 中发送 HTTP/2 POST 请求

php - if 语句在返回值方面无法正常工作 - PHP

php - 在 android 中访问服务器文件的本地主机路径是什么?

php - 在我的计算机上使用 PHP 从 MySQL InnoDB 数据库 [XAMPP] 获取结果比在其他计算机上花费的时间长得多

php - 计算行数和值匹配数

mysql - 数据库并发

php - 为什么 json_encode 返回空括号?

php - 如何在两个不同的 Web 服务器(例如 xampp 和 tomcat)之间使用 session 值。

php - 局域网内访问php、mysql站点