[Beginner] questions about writing PHP into MySQL database

Source: Internet
Author: User
[Beginner's advice] the problem of writing PHP into the MySQL database now uses the PHP array to generate a combination of 6 numbers from 1 to 33, and about 1 million pieces of data are generated. TXT file in the following format: [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 4, 8] [1, 2, 3, 3, 4, 5, 9] [1, 2, 3, 4, 4, 5, 10] [1, [New User Consultation] questions about writing PHP into MySQL database
Now, the PHP array is used to generate a combination of 6 numbers from 1 to 33. about 1 million pieces of data are generated into a data. TXT file. the format is as follows:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 7]
[1, 2, 3, 4, 5, 8]
[1, 2, 3, 4, 5, 9]
[1, 2, 3, 4, 5, 10]
[1, 2, 3, 4, 5, 11]
[1, 2, 3, 4, 5, 12]
[1, 2, 3, 4, 5, 13]
[1, 2, 3, 4, 5, 14]
[1, 2, 3, 4, 5, 15]
[1, 2, 3, 4, 5, 16]
[1, 2, 3, 4, 5, 17]
[1, 2, 3, 4, 5, 18]
...............
Now we want to store such data into the database. now we have a database table HMK with the structure
"Hmkid", "int (11)", "NO", "PRI", "", "auto_increment"
"L1", "int (10) unsigned", "NO ","","",""
"L2", "int (10) unsigned", "NO ","","",""
"L3", "int (10) unsigned", "NO ","","",""
"L4", "int (10) unsigned", "NO ","","",""
"L5", "int (10) unsigned", "NO ","","",""
"L6", "int (10) unsigned", "NO ","","",""
How can I write a PHP program to directly write data into the database while the program is running without storing the txt document?
My thoughts
PHP code
  $ Db = new mysqli ("localhost", "admin", "admin", "ssq"); $ db-> query ("truncate table hmk "); $ info = array (1, 2, 3, 4, 5, 6); // $ info is a combined array generated in the middle. foreach ($ info as $ skey => $ value) {// echo 'My array is underline :'. $ key. "=> ". $ value."
"; $ Query =" insert into hmk (l1, l2, l3, l4, l5, l6) values ($ values )"; $ result = $ db-> query ($ query);} if (result) echo "good luck"; else echo "sorry"; $ db-> close ();

Question 2: How do I write a program that imports a TXT file to the database? I want L1 to store the first data, L2 to store the second data, and so on,

Thank you for your help. For more information about generating a 1-33 data combination program, see
PHP code
  $ A = range (1, 33); $ ar = combination ($ a, 6); // Evaluate the efficient 10-moving function combination ($ numArr, $ combineLen) {$ numCt = count ($ numArr); if ($ combineLen> $ numCt) return; $ bin = str_pad ('', $ combineLen, '1 '); $ bin = str_pad ($ bin, $ numCt, '0', STR_PAD_RIGHT); $ find = $ bin; $ rs [] = implode ('', array_slice ($ numArr, 0, $ combineLen); $ j = 1; while (strrev ($ find )! = $ Bin) {$ k = explode ('10', $ find, 2); $ find = $ find {0 }== '0 '? Strrev ($ k [0]). '01 '. $ k [1]: $ k [0]. '01 '. $ k [1]; for ($ I = 0; $ I <$ numCt; $ I ++) $ rs [$ j]. = $ find [$ I]? $ NumArr [$ I]. "": ''; $ j ++;} return $ rs ;}


------ Solution --------------------
PHP code
$ Fp = fopen('data .txt ', 'r'); while (! Feof ($ fp) {$ line = trim (fget ($ fp); $ sqlvalue. = '('. substr ($ line, 1,-1 ). '),';} $ SQL = "insert into hmk (l1, l2, l3, l4, l5, l6) values"; $ SQL = $ SQL. substr ($ sqlvalue,-1, 1); // remove the end, $ db = new mysqli ("localhost", "admin", "admin", "ssq "); $ result = $ db-> query ($ SQL); if (! $ Result) {echo $ db-> error (); var_dump ($ SQL );}
------ Solution --------------------
It is too inefficient to insert only one record for each query. you can insert only one record at a time. for example, you can insert 50000 records at a time:

PHP code
$ Data = array (1, 2, 3, 4, 5, 6), array (1, 2, 3, 4, 57); // you can use this data array, each element is an array of numbers $ idx = 0; while ($ idx <count ($ data)-1) {$ ar = array (); if ($ idx + 50000 <count ($ data) $ ar = array_slice ($ data, $ idx, 50000); else $ ar = array_slice ($ data, $ idx ); $ SQL = 'Insert INTO htm (l1, l2, l3, l4, l5, l6) values'; $ ar_values = array (); foreach ($ ar as $ v) $ ar_values [] = '('. join (',', $ v ). ')'; $ SQL. = join (',', $ ar_values); $ result = $ db-> query ($ SQL); $ idx + = 50000;} $ db-> close ();
------ Solution --------------------

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.