我对PHP不太陌生,试图从mysql数据库中获取数据并希望将其导出为excel。我也遵循this answer。
它有点旧,使用MYSQL连接代替,我希望它作为mysqli,我更改了如下代码
if (isset($_POST["submit"]))
{
$select = "SELECT * FROM `records`";
$export = mysqli_query($mysqli, $select);
//$fields = mysql_num_rows($export); // thanks to Eric
$fields = mysqli_num_fields($export); // by KAOSFORGE
$col_title="";
$data="";
for ($i = 0; $i < $fields; $i++) {
$col_title .= '<Cell ss:StyleID="2"><Data ss:Type="String">'.mysqli_fetch_field_direct($export, $i).'</Data></Cell>';
}
$col_title = '<Row>'.$col_title.'</Row>';
while($row = mysqli_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = '<Cell ss:StyleID="1"><Data ss:Type="String"></Data></Cell>\t';
} else {
$value = str_replace('"', '', $value);
$value = '<Cell ss:StyleID="1"><Data ss:Type="String">' . $value . '</Data></Cell>\t';
}
$line .= $value;
}
$data .= trim("<Row>".$line."</Row>")."\n";
}
$data = str_replace("\r","",$data);
header("Content-Type: application/vnd.ms-excel;");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
$xls_header = '<?xml version="1.0" encoding="utf-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author></Author>
<LastAuthor></LastAuthor>
<Company></Company>
</DocumentProperties>
<Styles>
<Style ss:ID="1">
<Alignment ss:Horizontal="Left"/>
</Style>
<Style ss:ID="2">
<Alignment ss:Horizontal="Left"/>
<Font ss:Bold="1"/>
</Style>
</Styles>
<Worksheet ss:Name="Export">
<Table>';
$xls_footer = '</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>1</SplitHorizontal>
<TopRowBottomPane>1</TopRowBottomPane>
</WorksheetOptions>
</Worksheet>
</Workbook>';
print $xls_header.$col_title.$data.$xls_footer;
exit;
}
但在第16行中却给了我错误,如下所示
$col_title .= '<Cell ss:StyleID="2"><Data ss:Type="String">'.mysqli_fetch_field_direct($export, $i).'</Data></Cell>';
让我知道是否有人可以帮助我解决问题。 谢谢
答案 0 :(得分:1)
您需要访问<meta name="csrf_token" content="{{ csrf_token() }}" />
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{route('check-email')}}",
method: 'post',
data: {
username: jQuery('#username').val(),
email: jQuery('#email').val()
},
dataType: "json",
success: function(result) {
// Some Code
}
});
属性,而不是尝试回显该对象。
public function checkEmail(Request $request)
{
if($request->username) {
$username = Members::where('member_nickname', $request->username)->count();
$email = Members::where('member_email', $request->email)->count();
if ($username > 0 && $email <=0) {
return response()->json(['success' => 1]);
}
if ($username <= 0 && $email <=0) {
return response()->json(['success' => 2]);
}
if ($email > 0 && $username <=0) {
return response()->json(['success' => 3]);
}
if ($email > 0 && $username > 0) {
return response()->json(['success' => 4]);
}
}
}
这将根据需要提供列名称。
阅读手册:https://php.net/manual/en/mysqli-result.fetch-field-direct.php