简体   繁体   中英

Remove unwanted lines while Content-Type: text/plain

I will describe my problem with code - it would be the best.

<?
include('configs.php');  
require_once 'DBQueries.php'; 
$con = mysql_connect( $db_host, $db_user, $db_pass );
mysql_query("SET NAMES 'cp1250'") or die('Could not set names');     
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  } 
mysql_select_db($db_dbname);     
$oUnexportedOrders = DBQueries::getInstance()->getUnexportedOrders();
header("Content-Type: text/plain");         
while ($aOrderExport = mysql_fetch_assoc ($oUnexportedOrders)){
    echo $aOrderExport['data'];  
}

What is happening:

  1. include some stuff
  2. connection to DB
  3. get data from DB
  4. IMPORTANT: set header as Content-Type: text/plain
  5. IMPORTANT: print text data with echo

Result:

**!!! There are 7 unwanted lines !!!**
line of data
line of data 
line of data
line of data
....

Expected result:

line of data
line of data 
line of data
line of data

- Expected is lines of data generated by echo inside the for, but without that 7 lines.

QUESTION:

How to do that, what to call when (etc.) to get rid of those unwanted lines?

Thank you.

ob_clean(); will clear out the output buffer, in conjuction with ob_start();

<?
ob_start();
include('configs.php');  
require_once 'DBQueries.php'; 
$con = mysql_connect( $db_host, $db_user, $db_pass );
mysql_query("SET NAMES 'cp1250'") or die('Could not set names');     
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  } 
mysql_select_db($db_dbname);     
$oUnexportedOrders = DBQueries::getInstance()->getUnexportedOrders();
ob_clean();
header("Content-Type: text/plain");         
while ($aOrderExport = mysql_fetch_assoc ($oUnexportedOrders)){
    echo $aOrderExport['data'];  
}

That should get rid of any unwanted extra whitespace from included files.

The blank lines don't come from the mysterious unknown. They're in your code somewhere. Check your files (including the files you include/require) for whitespace before your opening PHP tags and after your closing PHP tags. That whitespace will be passed to the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM