【问题标题】:I want to output as a pure csv but it output with HTML tags我想作为纯 csv 输出,但输出时带有 HTML 标签
【发布时间】:2016-05-23 05:30:51
【问题描述】:

我更新了代码,但是在数据之前得到了一些标签

<br />                                              
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>                                             
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span>

警告:调用 wpdb::prepare() 缺少参数 2 C:\wamp\www\wordpress\wp-content\plugins\sinetiks-schools\deped_rpci.php 在第 14 行并在 C:\wamp\www\wordpress\wp-includes\wp-db.php 中定义 上线1246

</th></tr>                                          
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>                                             
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>                                              
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>312032</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\wordpress\wp-admin\admin.php' bgcolor='#eeeeec'>..\admin.php<b>:</b>0</td></tr>                                               
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>331856</td><td bgcolor='#eeeeec'>require_once( <font color='#00bb00'>'C:\wamp\www\wordpress\wp-load.php'</font> )</td><td title='C:\wamp\www\wordpress\wp-admin\admin.php' bgcolor='#eeeeec'>..\admin.php<b>:</b>31</td></tr>                                                

这是代码

<?php if (isset($_POST['exp'])) {

$wpdb->show_errors(); 

 global $wpdb;

 // Grab any post values you sent with your submit function
 $DownloadReportFrom = "ReportDateFrom";
 $DownloadReportTo = "ReportDateFrom";

 // Build your query                        
 $MyQuery = $wpdb->get_results($wpdb->prepare('SELECT * FROM wp_rpci_rpci'));


 // Process report request
 if (! $MyQuery) {
  $Error = $wpdb->print_error();
   die("The following error was found: $Error");
  } else {
 // Prepare our csv download

 // Set header row values
 $csv_fields=array();
 $csv_fields[] = 'Field Name 1';
 $csv_fields[] = 'Field Name 2';
 $csv_fields[] = 'Field Name 1';
 $csv_fields[] = 'Field Name 2';
 $csv_fields[] = 'Field Name 1';
 $csv_fields[] = 'Field Name 2';
 $output_filename = 'MyReport_' . $DownloadReportFrom .'-'.   $DownloadReportTo  . '.csv';
 $output_handle = @fopen( 'php://output', 'w' );

 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=' . $output_filename );
 header( 'Expires: 0' );
 header( 'Pragma: public' );    

 // Insert header row
 fputcsv( $output_handle, $csv_fields );

 // Parse results to csv format
 foreach ($MyQuery as $Result) {
$leadArray = (array) $Result; // Cast the Object to an array
// Add row to file
fputcsv( $output_handle, $leadArray );
}

// Close output file stream
fclose( $output_handle ); 

die();
exit();
}
 }
?>

我得到一些表格,然后它在表格标签之后显示数据库行

【问题讨论】:

  • 你得到了什么输出?以及您想要解决的问题,我的意思是请清除您的问题
  • 它输出 HTML 标签而不是表格内容

标签: php wordpress csv


【解决方案1】:

我搞定了 :)

我只是消除多余的论点

这是代码

$wpdb->show_errors(); 
global $wpdb;                       
// Grab any post values you sent with your submit function
$DownloadReportFrom = "ReportDateFrom";
$DownloadReportTo = "ReportDateFrom";

// Build your query 
// $MyQuery = $wpdb->get_results($wpdb->prepare('SELECT * FROM wp_rpci_rpci')); -Delete the ($wpdb->prepare                 
$MyQuery = $wpdb->get_results('SELECT * FROM wp_rpci_rpci');



// Process report request
if (! $MyQuery) {
$Error = $wpdb->print_error();
die("The following error was found: $Error");
} else {
// Prepare our csv download

// Set header row values
$csv_fields=array();
$csv_fields[] = 'Field Name 1';
$csv_fields[] = 'Field Name 2';

$output_filename = 'MyReport_' . $DownloadReportFrom .'-'. $DownloadReportTo  . '.csv';
$output_handle = @fopen( 'php://output', 'w' );

 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=' . $output_filename );
header( 'Expires: 0' );
header( 'Pragma: public' ); 

// Insert header row
fputcsv( $output_handle, $csv_fields );

// Parse results to csv format
 foreach ($MyQuery as $Result) {
$leadArray = (array) $Result; // Cast the Object to an array
// Add row to file
fputcsv( $output_handle, $leadArray );
}

// Close output file stream
 fclose( $output_handle ); 

 die();

 }

【讨论】:

    【解决方案2】:

    您不使用 mysqli_query,而是使用 wpdb::query。

    wpdb::query 返回受影响/选择的行数时,您将整数传递给 mysqli_fetch_assoc 并且由于明显的原因它失败了,因为它期望 mysqli_result 而不是某个数字。

    你需要得到真正的mysqli连接,然后你的代码才能工作。

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-09
      • 2011-04-26
      相关资源
      最近更新 更多