how to export to excel from mysql database using php

Dear Experts,
I use PHP 7 and mysql database.
I searched on the web how to export data to excel , I found many old codes with mysql_connect...
I need to use mysqli not mysql

Do you recomend me some working code?
LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomas Helgi JohannssonCommented:
Hi!

The simplest way to export data to excel is to export to csv using mysqli like this

<?php 
$con = new mysqli($host, $user, $password, $database);
 
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}
$result = mysqli_query($con, 'SELECT * FROM table');
if (mysqli_num_rows($result) > 0) {
$fp = fopen('file.csv', 'w');
while ($row = mysqli_fetch_assoc($result)) {
  fputcsv($fp, $row);
}
fclose($fp);
}
?>

Open in new window


Regards,
   Tomas Helgi
BRMarketingAuthor Commented:
Dear Thomas, Thank you, but I already have a wonderful export as csv code ( thanks to Ray Paseur )

However I need to export as excel ( not come seperated values )
Do you have code for that? Thank you
Tomas Helgi JohannssonCommented:
Hi!

Something like this for writing to excel.

<?php 
$con = new mysqli($host, $user, $password, $database);
$filename = "sampledata.xls"; // File Name
// Download file
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Content-Type: application/vnd.ms-excel");
 
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}
$flag = false;
$result = mysqli_query($con, 'SELECT * FROM table');

if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_assoc($result)) {
if(!$flag) {
      // display field/column names as first row
      echo implode("\t", array_keys($row)) . "\r\n";
      $flag = true;
    }
    echo implode("\t", array_values($row)) . "\r\n";
}

}
?>

Open in new window


Regards,
   Tomas Helgi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

BRMarketingAuthor Commented:
Dear Tomas Helgi, thank you for the wonderful code. One more thing I'd like to ask you about this code.

the letters like Ö, İ, Ş , Ğ is not shown properly on the Excel. Instead of this letters, it shows ? ( question mark ) on the excel table?

How can I correct this? Thank you
Tomas Helgi JohannssonCommented:
Hi!

For that you will probably include correct language in the header of the PHP/HTTP response.
header("Content-Language: <your language>")

Open in new window


See further here on what other headers you could use.
http://php.net/manual/en/function.header.php
http://www.jonasjohn.de/snippets/php/headers.htm

Regards,
     Tomas Helgi
BRMarketingAuthor Commented:
Thank you so much Tomas Helgi Johannsson
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.