Mysql cannot be inserted when phpexcel reads excel. how can this problem be solved?

Source: Internet
Author: User
Phpexcel Read excel can not insert mysql manually filled in the data can be read and inserted into the database, when a large number of data can not begin to suspect that there is a problem with the data format, the excel into the csv format of the UTF-8 to read the insert, the SQL statement is output in mysql. the execution of the SQL statement is okay. why is it so strange that phpexcel cannot be inserted into mysql when reading excel?
The manually entered data can be read and inserted into the database, but not in large batches.
Start to suspect that there is a problem with the data format, the excel into the csv format of the UTF-8 to read the insert, or not
Therefore, the SQL statement is output.
Execute SQL statements in mysql.
Why is this so strange?

------ Solution --------------------
Why are no errors reported during batch import ??
------ Solution --------------------
Is there a part of the SQL statement. You have not detected it. Can I reduce the volume of imported data. If possible, there is a problem with some data organization SQL statements.
------ Solution --------------------
Import and check data in batches. for example, some strings contain double quotation marks.
------ Solution --------------------
Is there a conflict between the excel format and the reading time, and whether the numbers and string formats are separated.
------ Solution --------------------
The following code reads the excel data and inserts it into the database:
1. <? Php
2. function getmicrotime (){
3. list ($ usec, $ sec) = explode ("", microtime ());
4. return (float) $ usec + (float) $ sec );
5 .}
6.?>
1. <? Php
2. $ time_start = getmicrotime ();
3. include ("db. inc. php"); // connect to the database
4. $ db = new testcsv;
5.?>
6. <? Php
7. $ handle = fopen (”test.csv "," r ");
8. $ SQL = "insert into scores (idcard, names, num, sex, nation, score) values ('";
9. while ($ data = fgetcsv ($ handle, 1000, ",") {
10. $ num = count ($ data );
11. for ($ c = 0; $ c <$ num; $ c ++ ){
12. if ($ c ==$ num-1) {$ SQL = $ SQL. $ data [$ c]. "')"; break ;}
13. $ SQL = $ SQL. $ data [$ c]. "','";
14 .}
15. print "<br> ";
16. echo $ SQL. "<br> ";
17. $ db-> query ($ SQL );
18. echo "SQL statement execution successful! <Br> ";
19. $ SQL = "insert into scores (idcard, names, num, sex, nation, score) values ('";
20 .}
21. fclose ($ handle );
22. $ time_end = getmicrotime ();
23. $ time = $ time_end-$ time_start;
24. echo "Program Execution time:". $ time. "Seconds ";
25.?>
Address: http://www.phpnewer.com/index.php/Ymgx/detail/id/41

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.