Php imports excel into mysql database code

Source: Internet
Author: User
? Connect to the database file $ connectmysql_connect (localhost, admin, admin) ordie (link to the database failed !); Waiting for ($ i0;

? // Connect to the database file $ connect = mysql_connect ("localhost", "admin", "admin") or die ("failed to connect to the database! "); // Connect to the database (test) mysql_select_db (" testcg ", $ connect) or die (mysql_error (); $ temp = file (" test.csv "); // connect the excelfile in the format of .csv for ($ I = 0;

// Connect to the database file
$ Connect = mysql_connect ("localhost", "admin", "admin") or die ("failed to connect to the database! ");
// Connect to the database (test)
Mysql_select_db ("testcg", $ connect) or die (mysql_error ());

$ Temp = file ("test.csv"); // connect to the excelfile, in the format of .csv
For ($ I = 0; $ I {
$ String = explode (",", $ temp [$ I]); // obtain the value of each record in an EXCEL file in a loop
// Insert the values of each record in the EXCEL file into the database
$ Q = "insert into ceshi (name, num, dom) values ('$ string [0]', '$ string [1]', '$ string [2]'); ";
Mysql_query ($ q) or die (mysql_error ());

If (! Mysql_error ());
{
Echo "data imported successfully! ";
}
Echo $ string [4]. "\ n ";
Unset ($ string );
}
?>

--------------------------------------------------------------------------------
Phpexcel or pear is a component with an Excel class plug-in China.
All of them are used to process excel. The first two functions are quite powerful and support for encoding is also good. We recommend that you use pear, which is called spreadsheet reader.

Phpexcel is charged for resources, but not all excel files can be read.

You can go to pear.php.net
--------------------------------------------------------------------------------
Answer on the top floor ~~ By the way, the answer is! Hey
--------------------------------------------------------------------------------
AdO direct upload

Note that some characters may not be supported by mysql or ADO
--------------------------------------------------------------------------------
First, confirm the encoding of your database. Take UTF-8 as an example,
Open the excel file, save it, and select ". CSV" as the file.
Then open the. csv file with the compiler and save it as a UTF-8 csv file.
Then, you can use php's getcsv to open the php file (this ensures that some of your fields are included, resulting in parsing errors), and then import the parsed results to the database.
Then it's done.

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.