20 line statement implementation from Excel to MySQL transformation _php Foundation

Source: Internet
Author: User
Tags php and mysql php code
Since the PHP and MySQL has a certain understanding, I will be in the company's huge Excel database eyeing, want to secretly "share" the confidential business data, while pondering how to get the hand, while thinking about how to convert into a MySQL database, to a lot of PHP site forum to consult a master, There is no one result, one day suddenly imagine, why not try this method? The result is also good, all OK, below I will my experience with you completely share (maybe you have a better way)
1. First introduce the structure of this Excel database, and named E.xls, the field has 4 (40 are also the same), the field name is A,b,c,d, and then the Jm.xls to do decomposition:
1 Select All records in field A, select Copy
2 Create a new Excel database file, paste the copied data in a blank field (usually a), to ensure that other fields blank, save as A.xls
3 Save A.xls as A.txt (formatted as "Text file (Tab delimited)")
4 The data from the other 3 fields in E.xls continues to generate B.txt,c.txt and d.txt by 1 to 3 steps.
2. After completing the above steps, you have done most of the work, the following is done by PHP and MySQL, very simple:
1 to put the data in MySQL, you must establish a MySQL database file name is dbname (and the PHP code in the same dbname), including table Tbname (and tbname in the PHP code to be consistent), 4 Fields, Inc. char (MB), adds char (+), pri char (M), tel Char (100), size is adjusted according to the field size in Excel, or partial value loss may occur.
2 and then write the PHP code, is the most important, the code is as follows:
------txt2mysql.php---------
?
$inc =file ("A.txt");
$adds =file ("B.txt");
$pri =file ("C.txt");
$tel =file ("D.txt");
$i = 0;
Mysql_connect ();
while (strlen ($inc [$i]) >0)
{
$sql = "INSERT into tbname values (' $inc [$i] ', ' $adds [$i] ', ' $pri [$i] ', ' $tel [$i] ')";
$do =mysql_db_query ("dbname", $sql);
$i = $i +1;
Echo ' <br> ';
}
$s = "SELECT * from Tbname";
$gg =mysql_db_query ("dbname", $s);
$n =mysql_num_rows ($GG);
Mysql_close ();
Echo ' <br> ';
echo "added". $n. " Records ";
?>

Note: A.txt,b.txt,c.txt,d.txt and txt2mysql.php to be in the same directory
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.