How PHP exports a database to Excel

Source: Internet
Author: User

<?php
$fname = $_files[' MyFile ' [' name '];
$do = Copy ($_files[' MyFile ' [' tmp_name '], $fname);
if ($do)
{
echo "Import data Success <br>";
} else {
echo "";
}
?>
<form enctype= "Multipart/form-data" action= "<?php echo". $PHP _self. ""; ?> "method=" POST ">
Import CVS data <input name= "MyFile" type= "file" > <input value= "Commit" type= "submit" >

</form>
<?
error_reporting (0);
Import files in CSV format
$connect =mysql_connect ("localhost", "a0530093319", "123456") or Die ("could does connect to database");
mysql_select_db ("a0530093319", $connect) or Die (Mysql_error ());
$fname = $_files[' MyFile ' [' name '];
$handle =fopen ("$fname", "R");
while ($data =fgetcsv ($handle, 10000, ","))
{
$q = "INSERT INTO Test (code,name,date) VALUES (' $data [0] ', ' $data [1] ', ' $data [2] ')";
mysql_query ($q) or Die (Mysql_error ());

}
Fclose ($handle);
?>

Use PHP to export the database to Excel, test completely successful
PHP Code www.devdao.com
<?php
$DB _server = "localhost";
$DB _username = "root";
$DB _password = "";
$DB _dbname = "Ishop";
$DB _tblname = "Oi_mall_payment";

$savename = Date ("Ymjhis");
$Connect = @mysql_connect ($DB _server, $DB _username, $DB _password) or Die ("couldn ' t Connect.");
mysql_query ("Set Names ' GBK '");
$file _type = "Vnd.ms-excel";
$file _ending = "xls";
Header ("content-type:application/$file _type;charset=big5");
Header ("content-disposition:attachment; Filename= ". $savename.". $file _ending ");
Header ("Pragma:no-cache");

$now _date = Date ("Y-m-j h:i:s");
$title = "Database name: $DB _dbname, Data sheet: $DB _tblname, Backup date: $now _date";

$sql = "SELECT * from $DB _tblname";
$ALT _db = @mysql_select_db ($DB _dbname, $Connect) or Die ("couldn ' t Select Database");
$result = @mysql_query ($sql, $Connect) or Die (Mysql_error ());

Echo ("$title \ n");
$sep = "\ t";
for ($i = 0; $i < Mysql_num_fields ($result); $i + +) {
Echo Mysql_field_name ($result, $i). "\ t";
}
Print ("\ n");
$i = 0;
while ($row = Mysql_fetch_row ($result)) {
$schema _insert = "";
for ($j =0; $j <mysql_num_fields ($result); $j + +) {
if (!isset ($row [$j]))
$schema _insert. = "NULL". $sep;
ElseIf ($row [$j]! = "")
$schema _insert. = "$row [$j]". $sep;
Else
$schema _insert. = "". $sep;
}
$schema _insert = Str_replace ($sep. " $ "," ", $schema _insert);
$schema _insert. = "\ T";
Print (Trim ($schema _insert));
print "\ n";
$i + +;
}
return (true);
?>

How PHP exports a database to Excel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.