尝试生成要导出为excel文件的报表表单数据库,但是当数据库中有数据时,我一直得到0结果。任何人都可以看看代码,让我知道我错了什么。代码需要进入从4 +
运行php 5.4+的服务器<?php
//set date
$date = date("m/d");
$course_report = $_POST['course_report'];
$course_info = explode('~',$course_report);
$course_info[0]; // course_id
$course_info[1]; // course_date
$select = "SELECT DISTINCT * FROM registrants WHERE (paid='Y' AND course_id = '$course_info[0]' AND course_date = '$course_info[1]')";
$result = mysqli_query($dbc, $select);
// first get the header row
while($row = mysqli_fetch_assoc($result)) {
$course = $row['course'];
$coursedate = $row['course_date'];
$export = mysql_query($select);
$fields = mysql_num_fields($export);
}
// Get header
$fields = isset($_POST['fields']);
for ($i = 0; $i < $fields; $i++) {
$header .= mysqli_field_name($export, $i) . "\t";
}
// Then get all the rows of data
$export = isset($_POST['export']);
while($row = mysqli_fetch_row($result)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = isset($_POST['data']);
$data = str_replace("\r","",$data);
// Check for blank data set
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
// end building the data
$header = isset($_POST['header']);
$course = isset($_POST['course']);
$file_name = $course . "_downloaded_" . $date;
header("Content-type: application/x-msdownload");
//header("Content-Disposition: attachment; filename=Registrar_101.txt");
header("Content-Disposition: attachment; filename=$file_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
// }
?>
答案 0 :(得分:0)
这个说它只能得到一行,但它实际上会得到所有行。
// first get the header row
while($row = mysqli_fetch_assoc($result)) {
$course = $row['course'];
$coursedate = $row['course_date'];
$export = mysql_query($select);
$fields = mysql_num_fields($export);
}
然后,对于每一行,您都会用mysql_query()
莫名其妙地重新执行查询
在这一点上,我已经停止了寻找,因为如果脚本的其余部分完全没有意义的话,你真的需要清理这里的逻辑。
答案 1 :(得分:0)
我不确定你认为在“首先得到标题行”下发生了什么。查询正在从“注册人”表中检索记录。你认为“标题行”来自哪里?您是否认为您将使用列标题从查询中获取一行,即字段的名称?如果是这样,那不是它的工作原理。
然后在此循环中,您将为每条记录重新执行查询。为什么?那取得了什么成果?然后,您将获得此内部查询中字段数的计数,并将其放入$ fields中。但你永远不会使用这个值。在循环之后,你立即覆盖它。
你明白isset()会返回true还是false?你似乎认为它返回一个字符串。
$ _ POST获取从网页上提交的表单上的值。你似乎在想它会从你的查询或其他东西给你价值。
我不确定你要完成的是什么,但我认为你的程序的核心应该是这样的:
<?php
//set date
$date = date("m/d");
$course_id = $_POST['course'];
$course_date = $_POST['course_date'];
// You really should use mysqli_real_escape_string to escape input strings,
// but let's skip that point for now
// Also, it's generally bad form to use "select *", as then you have no idea what
// fields you will get back. Better to list exactly what you want.
$select = "SELECT DISTINCT * FROM registrants WHERE (paid='Y' AND course_id = '$course_id' AND course_date = '$course_date')";
// This assume the db is already open as $dbc
$result = mysqli_query($dbc, $select);
// build header row
$header = '';
$field_count = mysqli_num_field($result);
for ($i = 0; $i < $field_count; $i++) {
$finfo = mysqli_fetch_field_direct($result,$i);
$name = $finfo->name;
$header .= $name . "\t";
// I'm not familiar with a mysqli_field_name function and couldn't find it in the docs.
// Whatever.
}
$data = $header . "\n";
// Then get all the rows of data
while($row = mysqli_fetch_row($result)) {
$line = '';
foreach($row as $value) {
if (!isset($value) OR $value == "") {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
$line .= $value;
}
$data .= trim($line)."\n";
}
... and then whatever you need to do with this thing ...