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

  • Home
  • SEARCH
  • 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 8504467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:02:29+00:00 2026-06-11T02:02:29+00:00

Goal: To retrieve a single record (row) from a MySQL database for a user

  • 0

Goal:
To retrieve a single record (row) from a MySQL database for a user to download as a CSV via a browser interface (some e.g. button saying ‘fetch record’ based on users session ID, (which is as field in the database).

[They’ve just filled out a form, which is written to a database, along with session ID and I want them to be able to download their entry as a CSV]

I understand PDO is the way forward for security and efficacy (Although open to other suggestions), but I think I am either getting the syntax wrong, or missing something.

As far as I understand it I have to get the PDO to call the correct record, and then export it to CSV (2 steps).

I’ve tried mixing together bits of code I’ve found from similar questions here, but frankly it’s a mess – so not worth posting.

Any guidance / explanations of what I need to do would be 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-06-11T02:02:31+00:00Added an answer on June 11, 2026 at 2:02 am

    You could have MySQL create the CSV for you with a single query. As documented under SELECT ... INTO Syntax:

    Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:

    SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
      FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
      LINES TERMINATED BY '\n'
      FROM test_table;
    

    Then you merely need return the created file to the user, e.g. using readfile().

    However, to do it your current way, you would do something like:

    $dbh = new PDO("mysql:dbname=$dbname;charset=utf8", $username, $password);
    $qry = $dbh->query('SELECT a,b,a+b FROM test_table');
    
    $fp = fopen('php://output', 'w');
    while ($row = $qry->fetch(PDO::FETCH_NUM)) fputcsv($fp, $row);
    fclose($fp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Goal: To retrieve information from a 3rd party database based off of a
Goal: Retrieve all instance of List<> from interface GameFactory Problem: I retrieve error message
Goal: Once i click on the start button on my user interface, i currently
Goal: Retrieve data aaa from the first td in tr by pressing the link
Goal: retrieve an element of data from within a PHP object by number. This
This is similar to: How to open a file using JavaScript? Goal: to retrieve/open
Goal: Produce an Excel document with information from 3 associated models that is similar
My goal is to retrieve the X,Y coords of a table. I have looked
My goal is to present a jsf page that has Create, Retrieve and Update
I need to retrieve the number of hours past midnight from a UIDatePicker control

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.