我是PHP和MySQL的新手。我知道其他语言,但是对于此项目,它是用PHP完成的。我在网上找到了一些需要调整的代码,因此它可以完成我的任务。我正在尝试从MySQL数据库导出数据,将其显示在屏幕上,并提供下载到excel的选项。该代码可以运行并且可以运行,但是它不会将来自SQL数据库的数据仅放在表列上。
<?php
$conn = new mysqli('localhost', 'root', '');
mysqli_select_db($conn, 'hcap');
$data = mysqli_query($conn,"SELECT `Org_ID`,'Org_Name', 'Org_Address', 'Org_Address2', 'Org_City', 'Org_State', 'Org_Zip', 'Org_County',
'Org_Website', 'Org_Phone', 'Org_fax', 'Org_Email'
FROM `organization`");
if(isset($_POST["ExportType"])) {
switch($_POST["ExportType"]) {
case "export-to-excel" :
// Submission from
$filename = $_POST["ExportType"] . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
ExportFile($data);
//$_POST["ExportType"] = '';
exit();
default :
die("Unknown action : ".$_POST["action"]);
break;
}
}
function ExportFile($records) {
$heading = false;
if(!empty($records))
foreach($records as $row) {
if(!$heading) {
// display field/column names as a first row
echo implode("\t", array_keys($row)) . "\n";
$heading = true;
}
echo implode("\t", array_values($row)) . "\n";
}
exit;
}
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<title>HCAP Organization Report</title>
<div><h3>HCAP Organization Report</h1></div>
<div>
<div id="container" >
<div class="col-sm-6 pull-left">
<div class="well well-sm col-sm-12">
<b id='project-capacity-count-lable'><?php echo count($data);?></b> records found.
<div class="btn-group pull-right">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu" id="export-menu">
<li id="export-to-excel"><a href="#">Export to excel</a></li>
<li class="divider"></li>
<li><a href="#">Other</a></li>
</ul>
</div>
</div>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="export-form">
<input type="hidden" value='' id='hidden-type' name='ExportType'/>
</form>
<table id="" class="table table-striped table-bordered">
<tr>
<th>Organization ID</th>
<th>Organization Name</th>
<th>Organization Address</th>
<th>Organization Address 2</th>
<th>City </th>
<th>State </th>
<th>Zip Code </th>
<th>County </th>
<th>Website </th>
<th>Phone Number </th>
<th>Fax Number </th>
<th>E-Mail Address </th>
</tr>
<tbody>
<?php foreach($data as $row):?>
<tr>
<td><?php echo $row ['Org_ID']?></td>
<td><?php echo $row ['Org_Name']?></td>
<td><?php echo $row ['Org_Address']?></td>
<td><?php echo $row ['Org_Address2']?></td>
<td><?php echo $row ['Org_City']?></td>
<td><?php echo $row ['Org_State']?></td>
<td><?php echo $row ['Org_Zip']?></td>
<td><?php echo $row ['Org_County']?></td>
<td><?php echo $row ['Org_Website']?></td>
<td><?php echo $row ['Org_Phone']?></td>
<td><?php echo $row ['Org_fax']?></td>
<td><?php echo $row ['Org_Email']?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
jQuery('#export-menu li').bind("click", function() {
var target = $(this).attr('id');
switch(target) {
case 'export-to-excel' :
$('#hidden-type').val(target);
//alert($('#hidden-type').val());
$('#export-form').submit();
$('#hidden-type').val('');
break
}
});
});
</script>
答案 0 :(得分:0)
您使用单引号而不是反引号。这就是为什么您在希望看到数据库值的地方看到文字列名称的原因。
改为使用此:
start <= x < finish
或者只是不使用任何引号(因为您的查询是不必要的-无需担心保留关键字或特殊字符)。
我在本地主机上测试了此建议的真实性和准确性。