View Single Post
id10t id10t is online now
Registered
 
id10t's Avatar
 
Join Date: Mar 2003
Posts: 10,459
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 07:32 PM..
Old 07-09-2014, 05:20 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #4 (permalink)