上下文:
我正在使用以下堆栈: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[]
和$row
中的一个/两个。感谢我在此错误上可以获得的任何帮助。谢谢您的宝贵时间!
图片:
代码:
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)
);
答案 0 :(得分:0)
您在 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)
祝你好运!