1

I would like to create an CSV file using php with an semicolon at the end of each line.

The file should looke like this:

Smith,"Andreas",";"
Smith,"John",";"

I can not get the semicolon at the end of the line. I tried all sorts of masking, read articels in the net but I just can't get it to work.

That is the code I use:

$datei1 = fopen($file1,"w");

$query = "SELECT * FROM customers ORDER BY cust_name";
$cust_name = $row['cust_name'];
$cust_firstname = $row['cust_firstname'];


$semicolon = ";";

$data = '"'.$cust_name.'","'.$cust_firstname.'","'.$semicolon.'"';

fwrite($datei1, $data);
fwrite($datei1, "\r\n");
7
  • 2
    Seems ok to me, what output are you getting that doesn't match your expectations? Commented Mar 21, 2018 at 13:08
  • Hi Federio, ist just shows the 1st line and stops when it detects the first semicolon. The output looks like adam,"andreas"," and all the other records are not written to the file. Commented Mar 21, 2018 at 13:15
  • 2
    Why not create an array, and use fputcsv? Commented Mar 21, 2018 at 13:16
  • No errors, no warnings, nothing? Also, if it stops after the first line it's because you're not looping over results. Commented Mar 21, 2018 at 13:17
  • No error messages at all. I guess php stops when it detects the first semicolon. I tried to mask it, but I could not get it to work. Commented Mar 21, 2018 at 13:19

1 Answer 1

2

I dont think semicolon is a problem here, it shows only the first line because you don't loop on your queries

$datei1 = fopen($file1,"w");

$data = mysql_query("SELECT * FROM customers ORDER BY cust_name",$db);

while($row = mysql_fetch_array($data)){
    $mydata = [ $row['cust_name'] , $row['cust_firstname'] , ';'];
    fputcsv($datei1,$mydata,",");
}

fclose($datei1);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Gourgandine, you are absolutely right. Your script did the trick! Thank you ever so much for your help. I really do appreciate it. Kind regards, Imbi

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.