如何将表数据导出到csv wordpress
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将表数据导出到csv wordpress相关的知识,希望对你有一定的参考价值。
我尝试导出用户数据但我们得到致命错误如何将所有用户注册数据提取为csv / excel格式我正在使用此链接的代码visit here
答案
试试这个,
$header_row = array(
0 => 'Display Name',
1 => 'Email',
2 => 'Institution',
3 => 'Registration Date',
);
$data_rows = array();
global $wpdb, $bp;
$users = $wpdb->get_results( "SELECT ID, user_email, user_registered FROM {$wpdb->users} WHERE user_status = 0" );
foreach ( $users as $u ) {
$row = array();
$row[0] = bp_core_get_user_displayname( $u->ID );
$row[1] = $u->user_email;
$row[2] = xprofile_get_field_data( 2, $u->ID );
$row[3] = $u->user_registered;
$data_rows[] = $row;
}
$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( "Content-Disposition: attachment; filename={$filename}" );
header( 'Expires: 0' );
header( 'Pragma: public' );
fputcsv( $fh, $header_row );
foreach ( $data_rows as $data_row ) {
fputcsv( $fh, $data_row );
}
fclose( $fh );
die();
希望这会对你有所帮助。
欲获得更多信息,
- WordPress script for doing a .csv export of misc data
- Export wordpress DB table to excel
- export to csv wordpress
另一答案
我的解决方案他们的关键是清除ob缓存。相应地更改表和字段类型。
function Export()
{
global $wpdb;
// Use headers so the data goes to a file and not displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
// clean out other output buffers
ob_end_clean();
$fp = fopen('php://output', 'w');
// CSV/Excel header label
$header_row = array(
0 => 'Email Address',
1 => 'First Name',
2 => 'Last Name',
);
//write the header
fputcsv($fp, $header_row);
// retrieve any table data desired. Members is an example
$Table_Name = $wpdb->prefix.'members';
$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name", 1) ;
$rows = $wpdb->get_results($sql_query, ARRAY_A);
if(!empty($rows))
{
foreach($rows as $Record)
{
$OutputRecord = array($Record['Email'],
$Record['FirstName'],
$Record['LastName']);
fputcsv($fp, $OutputRecord);
}
}
fclose( $fp );
exit; // Stop any more exporting to the file
}
以上是关于如何将表数据导出到csv wordpress的主要内容,如果未能解决你的问题,请参考以下文章
如何将表中的数据作为 CSV 从 Greenplum 数据库导出到 AWS s3 存储桶
在 oracle 12.1 中如何使用 sqlplus 将表数据导出到 csv 文件中