2

Thanks the community I was able to create a script that enable me to export data from a database table into a csv file.

"Article ID",Shoppergroupname,"Promotion Price",VAT-Code,"Article Currency","Promotion Start Date","Promotion End Date"
10192605,"Aucun Groupe Clients",2.86,2,27,07.01.2010,30.12.2999
10192689,"Aucun Groupe Clients",3.33,2,27,07.01.2010,30.12.2999
10193415,"Aucun Groupe Clients",2.40,1,27,07.01.2010,30.12.2999
10193431,"Aucun Groupe Clients",7.83,1,27,07.01.2010,30.12.2999

I would like my data being exported without quotes enclosure and values|fields being separated with ; instead of ,.

I would appreciate if someone could advise me how to achieve this.

Thanks a lot in advance.

Cheers,

Marc

My script is the following :

// Create connection
$conn = new mysqli($databasehost, $databaseusername, $databasepassword, $databasename);

// Check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
 }

 echo "Connected successfully | ";

// Create filename
date_default_timezone_set('Europe/Zurich');
$today = date("Y-m-d-G-i-s"); 
$csvname = "WS_PRICE_IMPORT_".$today.".csv";
$csvfullname = '/var/.../'.$csvname; 

// Create a file pointer connected to the output stream
$output = fopen($csvfullname, 'w');

// Output the column headings
fputcsv($output, array('Article ID', 'Shoppergroupname', 'Promotion Price', 'VAT-Code', 'Article Currency', 'Promotion Start Date', 'Promotion End Date'));

// Fetch the data
$sql = "SELECT `Article ID`, `Shoppergroupname`, `Promotion Price`, `VAT-Code`, `Article Currency`, `Promotion Start Date`, `Promotion End Date` FROM jos_temppriceimport";

$result = $conn->query($sql);

if (!$result) {
   echo "Unable to execute query in the database : " . mysql_error();
exit;
}

if ($result->num_rows == 0) {
echo "No record found, no record to export in CSV.";
exit;
}

// Loop over the rows, outputting them
while ($row = $result->fetch_row()) fputcsv($output, $row);
marcq
  • 477
  • 3
  • 12

1 Answers1

0

Well if you had taken a look at the manual for the funciton fputcsv you would've found this:

fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )

So for you code this means:

while ($row = $result->fetch_row()) fputcsv($output, $row, ";", " ");

Source: http://php.net/manual/en/function.fputcsv.php

Daan
  • 12,099
  • 6
  • 34
  • 51
  • Thank you for your suggestion. It works partially : `"Article ID",Shoppergroupname,"Promotion Price",VAT-Code,"Article Currency","Promotion Start Date","Promotion End Date"` `10192605; Aucun Groupe Clients ;2.86;2;27;07.01.2010;30.12.2999` Concerning the records, fields are separated now with a ";", I just have a indent before and at the end of the second field. Headers have still quotes and are separated with ";". – marcq Nov 02 '15 at 13:40
  • It tried this for the header, but it doesn't work `// Output the column headings` `$treatarray = array('Article ID', 'Shoppergroupname', 'Promotion Price', 'VAT-Code', 'Article Currency', 'Promotion Start Date', 'Promotion End Date');` `$array = implode(";", " ", $treatarray);` `fputcsv($output, $array);` – marcq Nov 02 '15 at 13:41
  • 2
    While this does remove the quote enclosure, it puts a space in its place, which could be undesirable. And unfortunately, `""` is not a valid enclosure. There are some other solutions [here](http://stackoverflow.com/q/1800675/353278). – Jeff Feb 08 '17 at 20:11