I use a couple of different things to achieve this - if the information is in a tabular format, by far the easiest way is to use the DataTables plugin (https://datatables.net/). Along with its core purpose of creating sortable, searchable tables you can easily add export & print buttons too.
I also use DWZone's CSV Import/Export plugin although currently only for importing CSV files to a database, haven't looked at the Export function: https://www.dwzone-it.com/Extension/PhpImportExportTools/How_WorkCsvWriter.asp.
I have both these tools running on PHP8.2 with no issues.
I also found this method using Javascript which you could populate using recordset info: https://www.javatpoint.com/javascript-create-and-download-csv-file