-4

I try to export user data but we get fatal error how to extract all user register data into csv/excel format i am usingcode of this link visit here

1
  • 5
    Please update your question with the error message that you are getting. Commented Jan 18, 2018 at 7:58

2 Answers 2

1

My solution. They key is the clean the ob caching. Change the table and field type accordingly.

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
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, Your key point is working for me. Thank you very much
0

Try this,

$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();

Hope this will helps you.

For more information,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.