Forum Moderators: coopster

Message Too Old, No Replies

export mysql data web results to ms excel

         

wool01

2:45 am on Mar 4, 2010 (gmt 0)

10+ Year Member



Hi everyone.. I need help for my form. I know this prob has a relevant topics.. like this one [webmasterworld.com...] but i think we have diff approach to the form and im stuck up with this. i have generated my mysql result in a browser so it displays the result. point is i need the displayed result to export in ms-excel.. im not a php programmer so i need some ideas.. thanks..

dreamcatcher

9:53 am on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi wool01, a warm welcome to WebmasterWorld. :)

Check out the PHP Classes Repository, you should find some free classes there to get you started.

[phpclasses.org...]

dc

andrewsmd

7:30 pm on Mar 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either that or you can just create a CSV file. I don't know how much functionality you need with excel but users can always open a csv and then save it as an excel file. Here is a script to generate a csv file.

$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM table");

//create the file
$handle = fopen("filename.csv", "w+");

while($row = mysql_fetch_array($result))
{
if(!(fwrite($handle, $row['column1'].",".$row['column2'].",".$row['column3']."\n"))){
echo("There was an error in writing to the file");
}//if
}//while

fclose($handle);
mysql_close($con);

I didn't test it but it should work.

wool01

5:28 am on Mar 8, 2010 (gmt 0)

10+ Year Member



Hi.. thank you for replying to my post. Actually i have a working query to my database.. Now my prob is i need to create a link to download the generated web report to excel. I've seen some examples downloading reports direct from the database but mine is i need to query first and display the results on the web and then download the displayed results to excel..

andrewsmd

2:21 pm on Mar 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So what you need to do is use the query to display the results. Have a link that says click here to download. When they click that, you use the same query and generate a csv file on your server. You redirect them to a page to download that file and then delete the file when they are done downloading it. You can't download directly from the database, it has to be put into a file for them to download.

wool01

3:13 am on Mar 9, 2010 (gmt 0)

10+ Year Member



So what you need to do is use the query to display the results. Have a link that says click here to download. When they click that, you use the same query and generate a csv file on your server. You redirect them to a page to download that file and then delete the file when they are done downloading it.


Yes you are right for what you said.. How will i do this?

andrewsmd

2:29 pm on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will do it in the manner I posted earlier. I'm not going to write the whole thing. You need to run that code on some sort of user event i.e. a button or link click, and then after the file is created, redirect them to the file.

wool01

1:52 am on Mar 10, 2010 (gmt 0)

10+ Year Member



yeah i do understand that i will use some event to be able to download and export it to excel but the thing is in your code:
$con = mysql_connect("localhost","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM table");

//create the file
$handle = fopen("filename.csv", "w+");

while($row = mysql_fetch_array($result))
{
do i need to connect again to database to pull out those datas since i do have already a data generated on the web? sorry but i don't really understand this since im a newbie on php. so please be patient.. thanks