Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:48:20+00:00 2026-06-04T00:48:20+00:00

I have a database set up which accepts user registrations and their details etc.

  • 0

I have a database set up which accepts user registrations and their details etc. I’m looking to export the database to an excel file using php.

The problem I am having is that some of the entrants have entered foreign characters in, such as Turkish, which has been written into the database ‘incorrectly’ – as far as I have ascertained, the charset was likely set up incorrectly when it was first made.

I have made my code to export the database into excel (below) but I cannot get the Excel document to show correctly regardless of how I try to encode the data

<?php 

require_once('../php/db.php');

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Download.xls");
header("Pragma: no-cache");
header("Expires: 0");

$query =    "SELECT * FROM users";

$result = mysqli_query($link, $query);
if($result) {
    $count = mysqli_num_rows($result);
    for($i=0; $i<$count; $i++) {
        $field = mysqli_fetch_field($result);
        $header .= $field->name."\t";
        while($row = mysqli_fetch_row($result)) {
            $line = '';
            foreach($row as $value) {
                if((!isset($value)) OR ($value == "")) {
                    $value = "\t";
                } else {
                    $value = str_replace('"', '""', $value);
                    $value = '"'.$value.'"'."\t";
                }
                $line .= $value;
            }
            $data .= trim($line)."\n";
        }
        $data = str_replace("\r", "", $data);
        if($data == "") {
            $data = "\n(0) Records Found!\n";
        }
    }
    print mb_convert_encoding("$header\n$data", 'UTF-16LE', 'UTF-8');
} else die(mysqli_error());
?>

When I do this it comes up with an error when opening it up saying that Excel doesn’t recognise the file type, it opens the document but its drawn boxes around all the Turkish characters its tried to write.

I’m no PHP expert this is just information I’ve kind of pieced together.

Can anyone give me a hand?

Much appreciated

Moz

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:48:21+00:00Added an answer on June 4, 2026 at 12:48 am
    1. First of all, you appear to be creating a tab-delimited text file and then returning it to the browser with the MIME-type application/octet-stream and the file extension .xls. Excel might work out that’s tab-delimited (but it sounds from your error as though it doesn’t), but in any case you really should use the text/tab-separated-values MIME type and .txt file extension so that everything knows exactly what the data is.

    2. Secondly, to create tab-delimited files, you’d be very wise to export the data directly from MySQL (using SELECT ... INTO OUTFILE), as all manner of pain can arise with escaping delimiters and such when you try to cook it yourself. For example:

       SELECT * FROM users INTO OUFILE '/tmp/users.txt' FIELDS TERMINATED BY '\t'
      

    Then you would merely need to read the contents of that file to the browser using readfile().

    If you absolutely must create the delimited file from within PHP, consider using its fputscsv() function (you can still specify that you wish to use a tab-delimiter).

    1. Always use the .txt file extension rather than .csv even if your file is comma-separated as some versions of Excel assume that all files with the .csv extension are encoded using Windows-1252.

    2. As far as character encodings go, you will need to inspect the contents of your database to determine whether data is stored correctly or not: the best way to do this is to SELECT HEX(column) ... in order that you can inspect the underlying bytes. Once that has been determined, you can UPDATE the records if conversions are required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose the following: I have a database set up on database.mywebsite.com , which resolves
Looking to have a database query set all the instance variables in a class:
Quick question I have a database with snapshot enabled using ALTER DATABASE myDB SET
I have a legacy database set with NLS_LANG set to IW8ISO8859P8. This I cannot
I have a MySQL database set-up in a hierarchy style. There are 4 tables
I have an SQL database with a set of tables that have Unique Id's.
Suppose I have a database like this: This is set up to give role-wise
I have sql database stored on a shared netwrok drive , after set of
I have a SQLite database that contains a huge set of log messages. I
I have a SQL database that has a table with a field set to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.