PHP import Excel file to MySQL database method, _php tutorial

Source: Internet
Author: User
Tags php programming

PHP to import Excel files to the MySQL database method,


This example describes how PHP imports Excel files into the MySQL database. Share to everyone for your reference. The specific analysis is as follows:

PHP import Excel file into MySQL database we need to use a Phpexcel class file, with this class file we can quickly and easily import Excel into MySQL database, here to give an example for you to explain the specific usage.

Before importing we need to prepare a database, the SQL statement code is as follows:
Copy CodeThe code is as follows:/*
Navicat MySQL Data Transfer

Source Server:localhost
Source Server version:50133
Source host:localhost:3306
Source Database:test

Target Server Type:mysql
Target Server version:50133
File encoding:65001

Date:2011-10-11 14:11:38
*/

SET foreign_key_checks=0;
-- ----------------------------
--Table structure for ' execl '
-- ----------------------------
DROP TABLE IF EXISTS ' execl ';
CREATE TABLE ' execl ' (
' id ' int (one) not NULL auto_increment,
' Name ' varchar (DEFAULT NULL),
PRIMARY KEY (' id ')
) Engine=myisam auto_increment=16 DEFAULT charset=latin1;

-- ----------------------------
--Records of EXECL
-- ----------------------------
INSERT into ' execl ' VALUES (' + ', ' Jim ');
INSERT into ' execl ' VALUES (' A ', ' Taurus ');
PHP processing program, here we need to use a Phpexcel class file, this can be Baidu search download, the code is as follows:
Copy CodeThe code is as follows: <?php
if ($_files[' execl ' [' name ']) {
$db = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Test ');
mysql_query (' Set names GBK ');
Require_once ' reader.php ';
$data = new Spreadsheet_excel_reader ();
$data->setoutputencoding (' CP936 ');
$data->read ($_files[' execl ' [' name ']);
Error_reporting (e_all ^ e_notice);
$sql = "";
for ($i =1; $i <= $data->sheets[0][' numrows '); $i + +)
{
if ($data->sheets[0][' cells '] [$i][1]!= "") {
$sql = "INSERT into ' execl ' (' name ') VALUES ('". $data->sheets[0][' cells ' [$i][2]. "";
if (mysql_query ($sql)) {
echo ' success ';
}else{
Die (' failure ');
}
}
}
}
?>




I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/942989.html www.bkjia.com true http://www.bkjia.com/PHPjc/942989.html techarticle PHP Import Excel file to MySQL database method, this article describes the PHP import Excel file to the MySQL database method. Share to everyone for your reference. The specific analysis is as follows: PHP import ...

  • Related Article

    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.