Export wordpress table to excel




Geniuswp show

Summary: 3 I have created PHP scripts before to export a database table to .xls format like this:$select = "SELECT * FROM tracking";$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );$fields = mysql_num_fields ( $export );$file = 'export';for ( $i = 0; $i < $fields; $i++ ){$header .= mysql_field_name( $export , $i ) . "\t";}while( $row = mysql_fetch_row( $export ) ){$line = '';foreach( $row as $value ){ if ( ( !isset( $value ) ) || ( $value == "" ) ) { $value = "\t"; } else { $value = str_replace( '"' , '""' , $value ); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim( $line ) . "\n"; } $data = str_replace( "\r" , "" , $data ); if ( $data == "" ) { $data = "\n(0) Records Found!\n"; }$filename = $file."_".date("M-d-Y");header("Content-type: application/octet-stream");header( "Content-disposition: filename=".$filename.".xls");header("Pragma: no-cache");header("Expires: 0");print "$header\n$data";What I'd like to know is how to "convert" this to wordpress format using the $wpdb class? I can get the initial query down to actually select the data from the table I want, but I get lost in some of the semantics of the wpdb class. I also wanted to know where I would put this code in wordpress, so if say a user clicks on a link, it would run this query and download the .xls file?Any help is greatly appreciated! This site is great! database wp-query query wpdb export asked Feb 26, 2011 at 3:19 Rob BennetRob Bennet30122 gold badges33 silver badges77 bronze badges 1 6 Why not use the SELECT INTO OUTFILE syntax:$wpdb->query("SELECT * INTO OUTFILE '/path/to/file' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' FROM tracking"); answered Feb 26, 2011 at 16:09 Derek DowneyDerek Downey26211 silver badge55 bronze badges 4 create this as a 'export_data.php' file.Then call this php from the link