Jump to content

how to create a button to export a mysql table to an excel file.


jbrill

Recommended Posts

excel or csv? csv is easier and imports perfectly

 

<?php
$q = mysql_query(SELECT * FROM TABLE) or die (mysql_error());
$row = mysql_fetch_array($q);
$rows = implode("\n",$row);
$data = implode(","$rows);
fwrite("File.csv",w);
echo "<a href=\"file.csv">Click here</a>";
?>

sorta i think

Link to comment
Share on other sites

I'm not sure if the implodes are right I just took a stab at it

try

<?php
while($row = mysql_rech_array($q)){
foreach($row as $value) {
$data[] = implode(",",$value);
}
}
$data = implode("\n",$data);
print_r($data);
$file = fopen("file.csv","w");
if(fwrite($file,$data)){
echo "Success";
}
else {
echo "Failure";
}
?>

Link to comment
Share on other sites

this worked for me: (check permissions on the subfolder)

<?php
$q = mysql_query("select * from myTable") or die(mysql_error());
while($row = mysql_fetch_array($q)){
$data[] = implode(",",$row);
}
$data = implode("\n",$data);
$file = fopen("file.csv","w");
if(fwrite($file,$data)){
echo "Success<br/>
<a href=\"file.csv\">Here</a>";
}
else {
echo "Failure";
}
?>

Link to comment
Share on other sites

i put this code in:

<?php
$q = mysql_query("select * from jobs") or die(mysql_error());
while($row = mysql_fetch_array($q)){
$data[] = implode(",",$row);
}
$data = implode("\n",$data);
$file = fopen("file.csv","w");
if(fwrite($file,$data)){
echo "Success<br/>
<a href=\"file.csv\">Here</a>";
}
else {
echo "Failure";
}
?>

 

and got this error:

 

 

Parse error: syntax error, unexpected T_STRING in /home/morow/public_html/admin/admin_options.php on line 44

 

line 44 is:

 

$q = mysql_query("select * from jobs") or die(mysql_error());

 

what permissions, on what folder?

 

Link to comment
Share on other sites

lets try this it shows all table you pick a table and then it csv it to a file called $table_unixtimestamp.csv

<?php

$sql = "SHOW TABLES FROM pira00_mysql";
$result = mysql_query($sql);
echo "<form action=\"csv.php\" method=\"post\">
<select name=\"table\">";
while ($row = mysql_fetch_row($result)) {
    echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select><input type=\"submit\" value=\"submit\">";
if (ISSET($_POST['table'])){
$table = $_POST['table'];
$q = mysql_query("select * from $table") or die(mysql_error());
while($row = mysql_fetch_array($q)){
$data[] = implode(",",$row);
}
$filename = $table;
$filename .= "_";
$filename .= date("U");
$filename .= ".csv";
$data = implode("\n",$data);
$file = fopen($filename,"w");
if(fwrite($file,$data)){
echo "<br/>Success<br/>
<a href=\"".$filename."\">".$filename."</a>";
}
else {
echo "Failure";
}
}
?>

Link to comment
Share on other sites

okay this works perfectly for me (reason it doubled was mysql_fetch_array draws associtive keys and indexed keys so i changed it to assoc and we are good) feel free to share but give me credit

<?php
/*
By Cooldude_832: (jdc22088@aol.com)
USE OF THIS WITHOUT THIS IS CONSIDERED STOLEN WORK!!!!!
this will allow you to see all your tables in a given mysql database (make connection first) 
and then select a database by giving $database a value.
Pick a value from the dropdown and hit submit.
Enojy.
Cooldude_832
*/
$database = "";
$sql = "SHOW TABLES FROM $database";
$result = mysql_query($sql);
echo "<form action=\"csv.php\" method=\"post\">
<select name=\"table\">";
while ($row = mysql_fetch_row($result)) {
    echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select><input type=\"submit\" value=\"submit\"></form>";

if (ISSET($_POST['table'])){
$table = $_POST['table'];
$q = mysql_query("SELECT * from $table") or die(mysql_error());
while($row3 = mysql_fetch_assoc($q)){
$data[] = implode(",",$row3);
}
echo "<br/><br/><br/><br/><br/>";
$data = implode("\n",$data);

$filename = $table;
$filename .= "_";
$filename .= date("U");
$filename .= ".csv";
echo $filename;
$file = fopen($filename,"w");
if(fwrite($file,$data)){
echo "<br/>Success<br/>
<a href=\"".$filename."\">".$filename."</a>";
}
else {
echo "Failure";
}
}
?>

Link to comment
Share on other sites

Okay I'm working with excel 2003 and i've tried different delimeters and it only likes the delimter of commas and \n so this version strips all , from the data which isn't great, but it will work until you rebuild it into a proper excel format.

<?php
/*
By Cooldude_832: (jdc22088@aol.com)
USE OF THIS WITHOUT THIS IS CONSIDERED STOLEN WORK!!!!!
this will allow you to see all your tables in a given mysql database (make connection first) 
and then select a database by giving $database a value.
Pick a value from the dropdown and hit submit.
Enojy.
Cooldude_832
*/
$database = "";
$sql = "SHOW TABLES FROM $databasel";
$result = mysql_query($sql);
echo "<form action=\"csv.php\" method=\"post\">
<select name=\"table\">";
while ($row = mysql_fetch_row($result)) {
    echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select><input type=\"submit\" value=\"submit\"></form>";

if (ISSET($_POST['table'])){
$table = $_POST['table'];
$q = mysql_query("SELECT * from $table") or die(mysql_error());
while($row = mysql_fetch_assoc($q)){

foreach($row as $value){
$row2[] = str_replace(","," ",$value);
}
$data[] = implode(",",$row);
unset($row2);
}
$data = implode("\n",$data);
print_r($data);
$filename = $table;
$filename .= "_";
$filename .= date("U");
$filename .= ".csv";
$file = fopen($filename,"w");
if(fwrite($file,$data)){
echo "<br/>Success<br/>
<a href=\"".$filename."\">".$filename."</a>";
}
else {
echo "Failure";
}
}
?>

Link to comment
Share on other sites

Okay I'm working with excel 2003 and i've tried different delimeters and it only likes the delimter of commas and \n so this version strips all , from the data which isn't great, but it will work until you rebuild it into a proper excel format.

<?php
/*
By Cooldude_832: (jdc22088@aol.com)
USE OF THIS WITHOUT THIS IS CONSIDERED STOLEN WORK!!!!!
this will allow you to see all your tables in a given mysql database (make connection first) 
and then select a database by giving $database a value.
Pick a value from the dropdown and hit submit.
Enojy.
Cooldude_832
*/
$database = "";
$sql = "SHOW TABLES FROM $databasel";
$result = mysql_query($sql);
echo "<form action=\"csv.php\" method=\"post\">
<select name=\"table\">";
while ($row = mysql_fetch_row($result)) {
    echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select><input type=\"submit\" value=\"submit\"></form>";

if (ISSET($_POST['table'])){
$table = $_POST['table'];
$q = mysql_query("SELECT * from $table") or die(mysql_error());
while($row = mysql_fetch_assoc($q)){

foreach($row as $value){
$row2[] = str_replace(","," ",$value);
}
$data[] = implode(",",$row);
unset($row2);
}
$data = implode("\n",$data);
print_r($data);
$filename = $table;
$filename .= "_";
$filename .= date("U");
$filename .= ".csv";
$file = fopen($filename,"w");
if(fwrite($file,$data)){
echo "<br/>Success<br/>
<a href=\"".$filename."\">".$filename."</a>";
}
else {
echo "Failure";
}
}
?>

 

ok, so i got the drop downs working and all but when i hit submit, it goes to the url:

 

http://myurl.com/admin/csv.php

 

where it says

 

"Not Found

The requested URL /admin/csv.php was not found on this server.

 

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. "

 

what do i have to do?

 

 

 

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.