Pelican Parts
Parts Catalog Accessories Catalog How To Articles Tech Forums
Call Pelican Parts at 888-280-7799
Shopping Cart Cart | Project List | Order Status | Help



Go Back   Pelican Parts Forums > Miscellaneous and Off Topic Forums > Off Topic Discussions


Reply
 
LinkBack Thread Tools Rate Thread
Author
Thread Post New Thread    Reply
Insert Tag Line HERE.....
 
rattlsnak's Avatar
 
Join Date: Nov 2000
Location: Atlanta, Ga
Posts: 9,679
Garage
Send a message via AIM to rattlsnak
MySql to .xls php script help

I have a MySql database that I need to be able to send to an .xls file that has multiple rows and columns. I would like to be able to generate a php script that will send a copy of the database to excel preferably from a onclick function so as the user can simply click a button to do it. I've looked at a few codes online, but everyone has a completely different way of doing it, so wondering if anybody here has a preferred way.

Thanks!
Marc

Old 07-08-2014, 07:47 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #1 (permalink)
The Unsettler
 
stomachmonkey's Avatar
 
Join Date: Dec 2002
Location: Lantanna TX
Posts: 23,885
Send a message via AIM to stomachmonkey
phpMyAdmin will export to CSV pretty much one click.

But users need access to it.

Set permissions properly and it should be ok.

Is the export an entire DB or the results of a query?
__________________
"I want my two dollars"
"Goodbye and thanks for the fish"
"Proud Member and Supporter of the YWL"
"Brandon Won"
Old 07-08-2014, 07:57 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #2 (permalink)
Insert Tag Line HERE.....
 
rattlsnak's Avatar
 
Join Date: Nov 2000
Location: Atlanta, Ga
Posts: 9,679
Garage
Send a message via AIM to rattlsnak
I setup up the database through phpmyadmin but the users will only have access to a website. Right now, I have setup a query based on a gui format where it can be searched and information entered by the users but also want an .xls file that I will use on google docs to share with other board members. I need it to be updated once a week or so hopefully from a oneclick button from one of the users. It will be the entire database each time, unless their is a way to update on the last few lines and export that to be added to the existing .xls?

In a nutshell, this is a database in which we allow a few users to enter penalties (local hockey club) into that can be later searched by the league officers and they want a local copy in an .xls format. So the users entering simply would have no clue or permissions to the database for anything else except data entry.
Old 07-08-2014, 08:23 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #3 (permalink)
Registered
 
id10t's Avatar
 
Join Date: Mar 2003
Posts: 10,317
Something like this should work. Took it from an existing setup I was using, removed the logic that specified some control over which rows were sent so it sends everything matching your query. Note that mysql_* functions are on the way out and should be replaced with PDO object calls instead.... If you can send me a mysql_dump of the table and the query you'd want to use (assuming you just don't want everything) and I'll do it up and debug it, then send you back the known-good PHP. Email it to me - sj at gruv dot org

PHP Code:
<?php

// written by id10t licensed under the GPLv2
// should be updated to use PDO 


    // the query your csv file will be based on
    
$query="select foo,bar,bee from somedbtable";

// what database is your table stored in
    
$dbName="your-database-name";

// your database host - get from ISP or your geek
    
$hostname="mysqlserver.example.com";

// db user credentials - get from ISP or your geek
    
$username="dbuser";
    
$password="secretwords";

// nothing to edit below this line ... unless you want to 

    // send response headers to the browser
    
header'Content-Type: text/csv' );
    
header'Content-Disposition: attachment;filename=SMART.csv');
    
$fp fopen('php://output''w');
    
    
MYSQL_CONNECT($hostname,$username,$password) or DIE("DATABASE FAILED TO RESPOND.");
    
mysql_select_db($dbName) or DIE("DB unavailable");
    
$result mysql_query($query) or die($query);
    
$number mysql_num_rows($result);
    
// output header row (if at least one row exists)
    
$row mysql_fetch_assoc($result);
    if(
$row) {
    
fputcsv($fparray_keys($row));
    
// reset pointer back to beginning
    
mysql_data_seek($result0);
    }
    for(
$i=0;$i<$number;$i++){
      
$row mysql_fetch_row($result);
      
fputcsv($fp$row);
    }
    
mysql_close();
    
fclose($fp);
?>
__________________
“IN MY EXPERIENCE, SUSAN, WITHIN THEIR HEADS TOO MANY HUMANS SPEND A LOT OF TIME IN THE MIDDLE OF WARS THAT HAPPENED CENTURIES AGO.”

Last edited by id10t; 07-10-2014 at 06:32 PM..
Old 07-09-2014, 04:20 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #4 (permalink)
Insert Tag Line HERE.....
 
rattlsnak's Avatar
 
Join Date: Nov 2000
Location: Atlanta, Ga
Posts: 9,679
Garage
Send a message via AIM to rattlsnak
Sent you an email.. Thanks!
Old 07-09-2014, 05:47 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #5 (permalink)
Registered
 
id10t's Avatar
 
Join Date: Mar 2003
Posts: 10,317
Replied
__________________
“IN MY EXPERIENCE, SUSAN, WITHIN THEIR HEADS TOO MANY HUMANS SPEND A LOT OF TIME IN THE MIDDLE OF WARS THAT HAPPENED CENTURIES AGO.”
Old 07-09-2014, 07:08 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #6 (permalink)
 
Insert Tag Line HERE.....
 
rattlsnak's Avatar
 
Join Date: Nov 2000
Location: Atlanta, Ga
Posts: 9,679
Garage
Send a message via AIM to rattlsnak
THANK YOU STEVE!! This board Rocks!!!
Old 07-10-2014, 06:28 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #7 (permalink)
Registered
 
id10t's Avatar
 
Join Date: Mar 2003
Posts: 10,317
Code had a minor bug in it (extra end parens left over from a removed print function), I've fixed the copy above. Also added some variable declaration and comments at the top since that is what rattlsnak had questions about.

Licensed under GPLv2 - use, change, share, enjoy

__________________
“IN MY EXPERIENCE, SUSAN, WITHIN THEIR HEADS TOO MANY HUMANS SPEND A LOT OF TIME IN THE MIDDLE OF WARS THAT HAPPENED CENTURIES AGO.”
Old 07-10-2014, 06:34 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #8 (permalink)
Reply


 


All times are GMT -8. The time now is 11:57 PM.


 
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0
Copyright 2025 Pelican Parts, LLC - Posts may be archived for display on the Pelican Parts Website -    DMCA Registered Agent Contact Page
 

DTO Garage Plus vBulletin Plugins by Drive Thru Online, Inc.