SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Threaded View

  1. #1
    SitePoint Zealot mike.fro's Avatar
    Join Date
    Jun 2005
    Location
    Lewisville, Texas
    Posts
    130
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    What am I doing wrong? MySQL DB to CSV Export

    Cannot figure out what the hell I'm doing wrong. Connecting to the database fine, can receive the .csv file via the browser but nothing is in the file. Can anyone spot what I might be missing?

    PHP Code:
    <?php
        
    function query_to_csv($db_conn$query$filename$attachment false$headers true) {
            
            if(
    $attachment) {
                
    // send response headers to the browser
                
    header'Content-Type: text/csv' );
                
    header'Content-Disposition: attachment;filename='.$filename);
                
    $fp fopen('php://output''w');
            } else {
                
    $fp fopen($filename'w');
            }
            
            
    $result mysql_query($query$db_conn) or die( mysql_error$db_conn ) );
            
            if(
    $headers) {
                
    // 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);
                }
            }
            
            while(
    $row mysql_fetch_assoc($result)) {
                
    fputcsv($fp$row);
            }
            
            
    fclose($fp);
        }


        
    // Using the function
        
    $sql "SELECT * FROM `leads` WHERE marketer = 'Michael Froseth'";
        
    // $db_conn should be a valid db handle
        
    $db_conn = new MySQLi('localhost''freesales''*****''freesalessystem');
        
    // output as an attachment
        
    query_to_csv($db_conn$sql"FREESALESSYSTEM-" date("Y-m-d") . ".csv"true);


        
    // output to file system
        
    query_to_csv($db_conn$sql"FREESALESSYSTEM-" date("Y-m-d") . ".csv"false);
    ?>
    Last edited by DaveMaxwell; Feb 20, 2013 at 10:44. Reason: wrapped code in php tags
    Michael John Froseth
    Creative Manager | Premier Planning
    Freelance Web Developer | MichaelFroseth | dotCom


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •