php,mysql to csv file?

Associate
Joined
19 Jul 2006
Posts
1,847
I have got the following code that works and displays a list of people from the database using a query on the previouse page.
Is it now possible to add a button that says export as csv file, and then export the results as a csv file?

PHP:
<?php
$link = mysql_connect('localhost', '****', '*******');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$Course=$_POST["category"];
mysql_select_db("misstudents", $link);
$query="SELECT * FROM misimport WHERE Course = '" . mysql_real_escape_string($_POST["category"]) . "'";  
$result = mysql_query ($query);

?> 

</head>

<body><div id="wrap"><div id="header"><h1>Moodle Upload Tool</h1>
</div> 
<div id="main">
 <?php echo $_POST["category"]; 
echo "<table border='2'>";
while($row = mysql_fetch_array($result))
  {
  echo "<tr><td>";
  echo $row['Id'];
  echo "</td><td>";
   echo $row['Id'];
  echo "</td><td>";
   echo $row['DOB'];
  echo "</td><td>";
   echo $row['Firstname'];
  echo "</td><td>";
   echo $row['Surname'];
  echo "</td><td>";
   echo $row['BACOLL'];
  echo "</td><td>";
   echo $row['Course'];
  echo "</td>";
   echo "</tr>";
    }
	echo "</table>";
?>

TIA
 
Cool, well, I always like the first option which offers the user the option to export to csv with a button. When they click this a query is run and it outputs the results to a set location. It's important to make this generic "like C:\export.csv". If you start wanting to export to desktops and stuff it gets messy.

I used this tutorial previously:

http://www.ineedtutorials.com/code/php/export-mysql-data-to-csv-php-tutorial

And set up the button as an onclick action. Basically, the button press runs a php file which has the query set up within it. I then just made several pages for different common query's each with a button.

If you want the button to dynamically change based on a query then I'm not too sure how that would work but its probably dooable. You would have to have functions that are set based on the same functions from the query I imagine.
 
Back
Top Bottom