2

While downloading the csv file it displaying the values in column wise. here is the code:

$id=1;
getPackage($certificate_id);
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="sample.csv"');
function getPackage($id){
$q_pack="select p.filename,p.version from cert c join packages p on c.package_id=p.id left join documents dt on p.id=dt.package_id left join documents doc on doc.id=d.document_id where c.certificate_id=$id";
$res_pack = db_query($q_pack);
if ($res_pack) {
while ($pack_row = db_fetch_object($res_pack)){
$fp = fopen('php://output', 'w');
foreach ( $pack_row as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);}
fclose($fp);}}

And also need to add add the header in row wise. Please find the screenshot how the csv file need to beCsv file

2
  • what do you want in header Commented May 2, 2017 at 13:22
  • @NavnitMishra header as Filename & Version Commented May 2, 2017 at 13:34

1 Answer 1

1

You have a lot of way, a simple way could be inject the value from the query as example:

$q_pack="select 'filename', 'version' from dual union select p.filename,p.version from cert c join packages p on c.package_id=p.id left join documents dt on p.id=dt.package_id left join documents doc on doc.id=d.document_id where c.certificate_id=$id";

Otherwise some other trick based on the first interaction of the extraction

Sign up to request clarification or add additional context in comments.

2 Comments

In csv file each row it getting the error like <div class='database-error'> & ******** SQL error ********, The used SELECT statements have a different number of columns</div><!--.
@Priya very strange: the first select extract two field same as the second (the constraint of the union is that the queries need to extract the same number of argument)

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.