1

I'm working on creating a script that takes a custom WordPress database table and exports it into a CSV file. I cannot seem to get it to work. Can someone help me out

<?php
$parse_uri = explode( 'wp-admin', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
global $wpdb;
$result = $wpdb->get_results("SELECT * FROM wp_email_table;");
$i = 0;
foreach ($result as $row) {
$csv_output .= $row->Field.
$i++;
}
$csv_output .= "\n";

$values = $wpdb->get_results("SELECT * FROM wp_email_table;");
foreach ($values as $rowr) {    
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr->$j.
}
$csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?> 

OK, so i modified it some more and now i receive the following error

Fatal error: Cannot use object of type stdClass as array on line 8

  • Please check this link and this link either of it shall give you a solution. – Prasad Nevase Dec 27 '15 at 12:12
  • Thanks @ItsMePN, i have tried the 2 links that you have provided and I still cannot get it to work. I have updated the code to reflect the changes but im still getting errors. – Phantom Dec 27 '15 at 13:03
  • $result vs. $results... – s_ha_dum Dec 27 '15 at 15:00
  • Thanks @jgraup i have corrected this (i updated the code here) and I get a completely different error: Fatal error: Cannot use object of type stdClass as array in – Phantom Dec 27 '15 at 15:22
  • I updated @jgraup but something tells me i didnt do it correctly. Can you let me know what I did wrong – Phantom Dec 27 '15 at 15:28
2

You've got a few things wrong.

  1. The first is that you are clearly in WordPress context. You should be using the AJAX API for this rather than including wp-load.php. That is virtually never the right way to do it.
  2. get_results() defaults to an object, so you are going to get errors when you try to use the results as an array. Add ARRAY_A to the argument list for that function (or use object syntax)
  3. Don't hard-code the WordPress table prefix
  4. $file isn't defined
  5. You have two queries when you only need one
  6. Your foreach and for, and associated code, is far too complicated.

I don't have your table to test with but this should be close.

function csv_pull_wpse_212972() {
  global $wpdb;
  $file = 'email_csv'; // ?? not defined in original code
  $results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}email_table;",ARRAY_A);

  if (empty($results)) {
    return;
  }

  $csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;

  foreach ($results as $row) {
    $csv_output .= '"'.implode('";"',$row).'";'."\n";
  }
  $csv_output .= "\n";

  $filename = $file."_".date("Y-m-d_H-i",time());
  header("Content-type: application/vnd.ms-excel");
  header("Content-disposition: csv" . date("Y-m-d") . ".csv");
  header( "Content-disposition: filename=".$filename.".csv");
  print $csv_output;
  exit;
}
add_action('wp_ajax_csv_pull','csv_pull_wpse_212972');

To "trigger" this send a request to http://sitename.com/wp-admin/admin-ajax.php?action=csv_pull. Within WordPress you can generate that (and should generate that) with $ajax_url = admin_url('admin-ajax.php?action=csv_pull');

|improve this answer|||||
  • Thanks @s_ha_dum that has gotten me close. Currently i get Warning: Cannot modify header information - headers already sent by... warning. Followed by a printed version of the CSV information. What I ultimately needed is to be able to call this function on button click to start a download of the CSV file.How can this be accomplished? – Phantom Dec 27 '15 at 17:27
  • hmm... I don't see anything that should cause that, and I got no errors in testing. Enable debugging (if you haven't) and see what you turn up. It may be some other code causing that issue. A number of hooks fire on admin-ajax.php – s_ha_dum Dec 27 '15 at 17:32
  • Yes I will take a look at that. In the meantime can you tell me how I would initiate the download on button click? – Phantom Dec 27 '15 at 17:35
  • @Phantom : see the edit – s_ha_dum Dec 27 '15 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.