0

I have a PHP script that is exporting a mysql query to a | delimited file. This working great except for the last "column" is appending a | pipe when I don't need it to.

$values = mysql_query("SELECT ColumnA AS Name, ColumnB AS Address, ColumnC AS Phone FROM Table1");
$row = 0;
while ($rowr = mysql_fetch_assoc($values)) 
{
  if ($row == 0)
  $row++;
  $csv_output .= "id|";
  foreach($rowr as $name => $value)
  {
  $csv_output .= $value."|";
  }   
      $csv_output .= "\n";    
}

The end result looks like

Name|Address|Phone|
Name|Address|Phone|
Name|Address|Phone|

How do I avoid having the ending pipe after the Phone| so it looks like this:

Name|Address|Phone
Name|Address|Phone
Name|Address|Phone

1 Answer 1

3

Use this one.

while ($rowr = mysql_fetch_assoc($values)) 
{
  if ($row == 0)
  $row++;
  $csv_output .= "id|";
  foreach($rowr as $name => $value)
  {
  $csv_output .= $value."|";
  }   
      $csv_output = substr($csv_output,0,-1);
      $csv_output .= "\n";    
}
Sign up to request clarification or add additional context in comments.

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.