简体   繁体   中英

PHP: Export sql data to CSV

I have an older PHP app that I am trying to figure out why I cannot export to CSV anymore.

On the page where sql data is displayed on the page the following link is displayed for exporting to CSV:

<p class="content_text"><strong><a href="export.php?tablename=PrivateActs109">Export to Excel</a></strong></p>

And then the entirety of the export.php file:

<?php /* ########## Connect to Database ########## */

require_once('../Connections/dbConnect_acts.php');
mysql_select_db($database_dbConnect, $dbConnect) or die ("no luck") ;



// Export to CSV
require '../includes/exportcsv.inc.php';
$table=$tablename; // this is the tablename that you want to export to csv from mysql.
$sql_query = "SELECT `ChapterNumber`, `Subject`,  `Abstract` , `BillNumber` FROM $tablename ";
exportMysqlToCsv($table,$sql_query);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");header("Content-Length: " . strlen($out));
// Output to browser with appropriate mime type, you choose ;)
//header("Content-type: text/x-csv");
//header("Content-type: text/csv");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");

?>

Looking for direction since I am still pretty new to PHP.

I wound up having to change the $table to the following:

$table=$_GET['tablename'];

Thanks for the suggestions, it is a legacy app and I learned a lot about PHP from working on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM