Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6745595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:13:26+00:00 2026-05-26T12:13:26+00:00

I got a Query that in one scenario I execute the query and it

  • 0

I got a Query that in one scenario I execute the query and it displays the results in a table format with data and headers etc… The second scenario I want to creat is a direct download link that will execute the query and put the results into a .csv file for saving or opening in Excel. I have the code for the display already but I am wondering how to make it download for the user.

Here is a tidbit of code for the execute and display.

    <?php
    $month = $_POST['mydate'];
    $monthfrm = date("Y-m-d", strtotime($month));
    $monthhd = date("F", strtotime($month));
    $monthto = date("Y-m-d", strtotime("$monthfrm . +1 month"));
    $date = date("F j,Y");
    if ($_POST['credsubmit']) {
       print('<head>');
       print('<link href="helper.css" media="screen" rel="stylesheet" type="text/css" />');
       print('<link href="dropdown.css" media="screen" rel="stylesheet" type="text/css" />');
       print('<link href="default.css" media="screen" rel="stylesheet" type="text/css" />');
    // Connecting, selecting database
       $dbconn = pg_connect("yeah we all know what goes here");
         if (!$dbconn) {
          die('Could not connect: ' . pg_last_error());
          }// Performing SQL query
     $query = "
       SELECT cm.\"ID\",a.\"ID\" as \"DDI\",cm.\"Date\",c.\"Amount\",ct.\"Name\" as \"Name\",cm.\"Comments\" as \"Comments\"
       FROM \"BIL_CreditMemo\" cm
       LEFT JOIN \"BIL_Credit\" c ON (c.\"ID\" = cm.\"BIL_CreditID\")
       LEFT JOIN \"ACT_Account\" a ON (c.\"ACT_AccountID\" = a.\"ID\")
       LEFT JOIN \"BIL_val_CreditMemoReason\" ct ON (ct.\"ID\" = cm.\"BIL_val_CreditMemoReasonID\")
       WHERE cm.\"Date\" >= '$monthfrm' AND cm.\"Date\" < '$monthto';
       ";
       $result = pg_query($query) or die('Query failed: ' . pg_last_error());
       // Printing results in HTML
       echo "<h2 align=center style=margin-top:20>Credit Memo Report for: $monthhd</h2>";
       echo "<table align=center width=60%>\n";
       print('<tr class="trstyle"><td>ID</td><td>DDI</td><td>Date</td><td>Amount</td><td>Name</td><td>Comments</td></tr>');
       while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
       echo "\t<tr>\n";
         foreach ($line as $col_value) {
           echo "\t\t<td>$col_value</td>\n";
         }
       echo "\t</tr>\n";
      }
      echo "</table>\n";// Free resultset
      pg_free_result($result);// Closing connection
      pg_close($dbconn);
      }
      ?>

So that is what I got so far and want to make the results a direct download to a csv file? Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:13:27+00:00Added an answer on May 26, 2026 at 12:13 pm

    You need to set a few HTTP headers, then you just echo out CSV data. For example,

    <?php
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    
    
    echo "Name,Favourite Colour\n";
    echo "Phil,Blue\n";
    ?>
    

    Note that if you’re specifically targeting Excel, then there are libraries that will allow you to create .xls files; I quite like PHPExcel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a SQL query that joins a pricing table to a table containing
I've got a query below. I want to add a condition that if the
I've got an sql query that selects data from several tables, but I only
Okay so i got one table with people. One table with items that are
I've got a query that returns the cost of wages for a given member
I've got a query that looks a bit like this: select records.id, contacts.name +
I've got a query that returns a proper result set, using SQL 2005. It
Right, so I've got a query that looks like this: $bestof_query = SELECT *
I've got an SQL query that looks like this: INSERT INTO DB..incident ( incident_number
I got this error running a query that goes against 2 tables with combined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.