How to import an excel file to a mysql database using php

Source: Internet
Author: User
This article mainly introduces how to import excel files from php to mysql databases, analyzes the skills of excel file operations in phpexcel and the methods for importing databases, which has some reference value, for more information about how to import an excel file from php to a mysql database, see the following example. Share it with you for your reference. The specific analysis is as follows:

To import an excel file from php to the mysql database, we need to use a phpexcel file. with this file, we can quickly and easily import an excel file to the mysql database, here is an example to illustrate the specific usage.

Prepare a database before import. the SQL statement code is as follows:

The 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 versions: 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 (11) not null AUTO_INCREMENT,
'Name' varchar (20) default null,
Primary key ('id ')
) ENGINE = MyISAM AUTO_INCREMENT = 16 default charset = latin1;

------------------------------
-- Records of execl
------------------------------
Insert into 'execl' VALUES ('14', 'Jim ');
Insert into 'execl' VALUES ('15', 'Taurus ');


Php processing program. here we need to use a phpexcel file, which can be searched and downloaded by Baidu. the code is as follows:

The 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 'successfully ';
} Else {
Die ('failed ');
}
}
}
}
?>




I hope this article will help you with php programming.

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.