0

Here is my code who works very well.When i call this file from browser it downloads my file in CSV format.

<?php
/* vars for export */
// database record to be exported
$db_record = 'people';
// optional where query
$where = 'WHERE 1 ORDER BY 1';
// filename for export
$csv_filename = 'exported.csv';
// database variables
$hostname = "localhost";
$user = "root";
$password = "";
$database = "db1";

// Database connecten voor alle services
mysql_connect($hostname, $user, $password)
or die('Could not connect: ' . mysql_error());

mysql_select_db($database)
or die ('Could not select database ' . mysql_error());
// create empty variable to be filled with export data
$csv_export = '';
// query to get data from database
$query = mysql_query("SELECT * FROM ".$db_record." ".$where);
$field = mysql_num_fields($query);
// create line with field names
for($i = 0; $i < $field; $i++) {
  $csv_export.= mysql_field_name($query,$i).';';
}
// newline (seems to work both on Linux & Windows servers)
$csv_export.= '
';
// loop through database query and fill export variable
while($row = mysql_fetch_array($query)) {
  // create line with field values
  for($i = 0; $i < $field; $i++) {
    $csv_export.= '"'.$row[mysql_field_name($query,$i)].'";';
  } 
  $csv_export.= '
';  
}
// Export the data and prompt a csv file for download
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=".$csv_filename."");
echo($csv_export);
?>

Assume that the files generated is like this:

"name", "age", "country"
"Gulizer", "21", "Kurdistan"
"Pierre", "20", "France"

What i want is retreive age again and put it at the and of each lines with some text like

"name", "age", "country", "text_added"
"Gulizer", "21", "Kurdistan", "The age of Gulizer is 21"

Is it possible?

Koçer
  • 35
  • 1
  • 5
  • what's wrong with SELECT INTO OUTFILE? – e4c5 Dec 05 '16 at 11:12
  • Nothing. It works great – Koçer Dec 05 '16 at 11:23
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Dec 05 '16 at 11:24
  • So how do you decide what text you want to add th this new column as well as the age – RiggsFolly Dec 05 '16 at 11:26
  • At the end you see csv exemple that I want generate. – Koçer Dec 05 '16 at 11:28
  • Yes but where does `Kurda` and `salî` come from in `"Kurda 21 salî"` – RiggsFolly Dec 05 '16 at 11:29
  • Ok I understand now what you ask. Just for Gulizer. "Kurda" and "salî" are thé text who will not change. Forget for Pierre "Français de ans" – Koçer Dec 05 '16 at 11:32
  • Thats no real help. You mean you just make this up as you go along. Thats not how computers work. This extra data has to be able to be logically associated with something in the result of your query – RiggsFolly Dec 05 '16 at 11:47
  • i modified. I just want add "The age is ['age']" at the end of the lines in csv – Koçer Dec 05 '16 at 12:23
  • so if SELECT INTO OUTFILE works why are you writing a whole heap of code – e4c5 Dec 05 '16 at 12:29
  • For better understanding. i am not a coder. i want just make work this file. And also for a better help because if anyone give me the solution i don't know where put the code given. I hope you understand with my bad english. i want juste put age with somme text a the and of each lines – Koçer Dec 05 '16 at 12:33

1 Answers1

0

I found the solution by myself and share with you

I added

echo "text_added;";

just after

for($i = 0; $i < $field; $i++) {$csv_export.= mysql_field_name($query,$i).';';}
$csv_export.= '\n';

and changed

for($i = 0; $i < $field; $i++) {
    $csv_export.= '"'.$row[mysql_field_name($query,$i)].'";';
  } 
  $csv_export.= '
';  
}

to

        for($i = 0; $i < $field; $i++) { $csv_export.= '"'.$row[mysql_field_name($query,$i)].'";'; }
$csv_export.= '
'.$row['age'].'years old";';
}

and finally i got this result:

text_added;name;age;country
"21 years old"; "Gulizer"; "21"; "Kurdistan"
"20 years old"; "Pierre"; "20"; "France";

exactly what i want.

Koçer
  • 35
  • 1
  • 5