如何将表数据导出到csv wordpress

rqdpfwrv  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(334)

我试图导出用户数据,但我们得到致命的错误如何提取到csv/excel格式的所有用户注册数据我使用这个链接访问这里的代码

vxf3dgd4

vxf3dgd41#

试试这个,

$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脚本,用于执行杂项数据的.csv导出
将wordpress数据库表导出到excel
导出到csv wordpress

wpx232ag

wpx232ag2#

我的解决方案。它们的关键是清除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
 }

相关问题