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.
You could have MySQL create the CSV for you with a single query. As documented under
SELECT ... INTOSyntax: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: