数据没有通过php从mysql完全导出到csv

tmb3ates  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(624)

上下文:
我正在使用以下堆栈:html/css、php、mysql和xampp来构建一个web应用程序,用于存储和检索输入到表单中的数据。
问题:
我有两个文件(i)export.php和(ii)export\u index.php。从下面的图片中可以看到,export_index.php文件对数据进行了索引,在web浏览器中本地查看时,它是mysql数据库中数据的完整表示。export.php文件应该输出到本地下载文件夹,与您在export\u index.php文件中看到的完全相同的内容,输出为.csv格式的文件,以便在excel中轻松打开。
在阅读了文档并试图调试它之后,我仍然不知道错误在哪里,但是我认为它来自于两者中的任何一个 $data[] 以及 $row . 谢谢你对我的帮助。谢谢你的时间!
图像:
index_export.php文件的输出

export.php文件的输出

代码:
索引\u 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>

导出.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);
    }
}

?>

创建.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)
);
dzjeubhm

dzjeubhm1#

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

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

在上述代码使用的情况下

if(count($data) > 0)

而不是

if(count($comp) > 0)

祝你好运!!!

相关问题