2

I am trying to generate invoice with the following code.

It works fine in local-host ,

When i executed the code in my friends server , it worked fine,

where as same code fails to generate in my server - (using godaddy)

Do i need to modify any server settings?

 <?php
    include("includes/DbConfig.php");
    $SQL = "SELECT id,user_mobile,user_email FROM users limit 1,2";
    $header = '';
    $result ='';
    $exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );

    $fields = mysql_num_fields ( $exportData );

    for ( $i = 0; $i < $fields; $i++ )
    {
        $header .= mysql_field_name( $exportData , $i ) . "\t";
    }

    while( $row = mysql_fetch_row( $exportData ) )
    {
        $line = '';
        foreach( $row as $value )
        {                                            
            if ( ( !isset( $value ) ) || ( $value == "" ) )
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
        }
        $result .= trim( $line ) . "\n";
    }
    $result = str_replace( "\r" , "" , $result );

    if ( $result == "" )
    {
        $result = "\nNo Record(s) Found!\n";                        
    }

        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=export.xls");
        header("Pragma: no-cache");
        header("Expires: 0");
        print "$header\n$result";
    ?>
Chaitanya K
  • 1,788
  • 6
  • 28
  • 39
  • 3
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 22 '16 at 13:09
  • 2
    Have you had a look at the error logs? – Jay Blanchard Jan 22 '16 at 13:09
  • i have checked the error log , no errors in it – Chaitanya K Jan 22 '16 at 13:12
  • How are you defining `TABLE_USERS`? Where is your constant definition? – Jay Blanchard Jan 22 '16 at 13:12
  • Is that file included in the above PHP? I do not see the include. – Jay Blanchard Jan 22 '16 at 13:16
  • yes its included....in my code... – Chaitanya K Jan 22 '16 at 13:17
  • 1
    Your previous comment says db_config.php, but you're showing DbConfig.php in the code. Which one is right? And you have changed your query from the original question. Stealth edits like that are *not cool*. – Jay Blanchard Jan 22 '16 at 13:18
  • the code which i have edited is the correct one.. table is getting connected , and data is also getting echoed..correctly..from the table... but the thing is excel is not getting generated... – Chaitanya K Jan 22 '16 at 13:20
  • Which browser are you using? – Jay Blanchard Jan 22 '16 at 13:24
  • Do you know if you have the permission to write onto the server? It might be worth checking the directory of which you are writing to allows for it to happen. – Richard Cripps Jan 22 '16 at 15:37
  • i have kept the file in root its permission : 0644 – Chaitanya K Jan 22 '16 at 16:06

1 Answers1

1

I just added ob_get_clean(); and it started working

<?php
ob_start();
session_start();
include("includes/DbConfig.php");
?>

   <?php
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=export.xls");
    ob_get_clean();
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$result";
    ?>
Chaitanya K
  • 1,788
  • 6
  • 28
  • 39