Jump to content

Recommended Posts

Hey guys,

 

How can I put 5 columns form MySQL to 5 columns in CSV? Not comma separated, but each every column for itself.

 

like this:

 

Column 1    Column 2    Column 3    Column 4    Column 5

Data 1      Data 2        Data 3      Data 4      Data 5

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/
Share on other sites

This is the result:

 

"kalkulacija_id kataloski_broj VPC MPC datum_kalkulacije naziv klasifikacija marza banka spedicija carina tecaj prosjecni_rabat max_rabat nabavana "

 

But, everything is generated in column A. I would like it to be :

 

kalkulacija_id - Column A

kataloski_broj - Column B

VPC - Column C

...

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734240
Share on other sites

Something like this maybe?

 

<?php

$host = 'localhost';
$user = 'xxxx';
$pass = 'xxxx';

$conn  = mysql_connect($host, $user, $pass);
$db = mysql_select_db('test', $conn);

$output = "";

$sql = "SELECT * FROM yourtable";
$res = mysql_query($sql, $conn);
$row = mysql_fetch_array($res);
$headers = mysql_num_fields($res);
$data = mysql_num_rows($res);

for($i = 0; $i<$headers; $i++) {
$colname = mysql_field_name($res, $i);
$output .= $colname."\t";
}

$output .= "\r\n";

do {

  for($i = 0; $i<$data; $i++) {
  $field = $row[$i];
  $output .= $field."\t";
  }
  
$output .= "\r\n";

} while ($row = mysql_fetch_array($res));

$fh = fopen('output.txt', 'w');
fwrite($fh, $output);

?> 

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734276
Share on other sites

Something like that, but I'd like to export it to Excel CSV file format.

 

Every column from MySQL should be in separate column in CSV.

 

This is the example:

 

MySQL columns: Part Number      Description      Location

to                          |                    |                    |

                            ˅                    ˅                  ˅

Excel columns:  Column A          Column B        Column C

 

Right now, the code from above is putting everything to Column A.

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734629
Share on other sites

1st of all, u r saying you want CSV, but at the same time you said you dont want comma separated. Its little bit confusing. CSV mean comma seperated value. When you open this CSV file with MS Excel, you will not see the commas, instead you will see each comma seperated value in a new column, what you want. And if you dont want comma seperated fiiles, then go for Tab Delimited File, not CSV because you can't change definition. But in both cases, either CSV or Tab Delimited, the end result will be same, when you open with excel, each seperated value in a line will be in a new column.

 

Anyway, It would be better if you post some portion of your code bacause those value should not be placed in the same column.

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734687
Share on other sites

OK, so here is the code I'm using:

 

<?php
include ("../admin/servis/include/session.php");

$baza = "servis";
$tablica = "kalkulacija";

$upit = "SELECT kataloski_broj, VPC, MPC FROM kalkulacija";
$rezultat = mysql_query($upit,$veza) or die (mysql_error());

$out = '';

//Dohvačanje naziva svih kolona
$fields = mysql_list_fields($baza,$tablica);

//Brojanje polja tablice i ubacivanje u $columns
$columns = mysql_num_fields($fields);

//Stavljanje imena svih polja u $out
for($i = 0; $i < $columns; $i++) {

$l = mysql_field_name($fields,$i);

$out .= "".$l."\t";
}

$out .= "\n";

//Dodavanje svih vrijednosti iz tablice u $out
while($l = mysql_fetch_array($rezultat)) {

//for($i = 0; $i < $columns; $i++) {
//$out .= '"'.$l["$i"].'"\t';
//}
$out .= $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\t";

$out .= "\n";

}

//Otvori file export.csv
$f = fopen('export.csv','w');

//Stavljanje svih vrijednosti u CSV
fputs($f,$out);
fclose($f);

header('Content-type:application/csv');
header('Content-Disposition:attachment; filename="export.csv"');
readfile('export.csv');

mysql_close($veza);

//header("Location:kalkulacija.php");
exit;
?>

 

But the result is put in one column. I want to separate every column for itself.

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734694
Share on other sites

I forgot to mention, whether you make it tab delimited and name it example.txt .... or make it comma seperated and name it example.csv, you will find the same result when you open it with Excel, exactly what you want. But dont mix those format, that is, dont make it tab delimited and name it example.csv or vice-versa.

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734698
Share on other sites

I guess making CSV or Tab delimited is much more easier and straight forward and at the same time it will surve your purpose. Do you try opening those files with Excel? You cant distinguish those with an excel file. Then why are you want to write a pure excel file (.xls) ?

 

Ofcourse you can't save any advance feature like Macro, Pivot table or formula etc in those files (though you can write it, just cant save it). If you really want to make a pure excel file, you need to use some classes. You can find many of those in internet like PhpExcel etc. I personaly use a user-defined class. But again I am teling you that, I go for pure excel on that particular case only because I need to attach some image, color some cells, make some formula ect. In all other cases where I just need the data to be stored, I always use CSV/tab delimited files.

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734703
Share on other sites

<?php

$host = 'localhost';
$user = 'xxxx';
$pass = 'xxxx';

$conn  = mysql_connect($host, $user, $pass);
$db = mysql_select_db('yourdb', $conn);

$output = "";

$sql = "SELECT * FROM yourtbl";
$res = mysql_query($sql, $conn);
$row = mysql_fetch_array($res);
$fields = mysql_num_fields($res);
$data = mysql_num_rows($res);

for($i = 0; $i<$fields; $i++) {
$colname = mysql_field_name($res, $i);
$output .= '"'.$colname.'"';
if($i != $fields - 1) {
	$output .= ",";
}
}

$output .= "\n";

do {

  for($i = 0; $i<$fields; $i++) {
  $field = $row[$i];
  $output .= '"'.$field.'"';
  if($i != $fields - 1) {
  	$output .= ",";
  }
  }
  
$output .= "\n";

} while ($row = mysql_fetch_array($res));

$fh = fopen('output.csv', 'w');
fwrite($fh, $output);

?>  

 

That will export any sized table to .csv by changing the table name in the select statement.

 

Well, and the database name :)

 

It will open properly in Excel

 

Edited the last time becaus eI forgot to wrap the fields with quotes... in case there are spaces.

Link to comment
https://forums.phpfreaks.com/topic/140308-solved-csv-export/#findComment-734789
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.