Importing mysql code using txt in PHP

Source: Internet
Author: User

Note that the encoding of the database tutorial should be the same as that of the txt document during import, or the garbled problem may occur.

<? Php Tutorial
// Verify the file type and error
If ($ _ FILES ["insert_first"] ["type"] = "text/plain "){
Echo "Step 1: the file type is correctly uploaded". "<br/> ";
If ($ _ FILES ["insert_first"] ["error"]) {
Echo "upload error:". $ _ FILES ["insert_first"] ["error"]. "<br/> ";
} Else {
Echo "File name:". $ _ FILES ["insert_first"] ["name"]. "<br/> ";
Echo "file type:". $ _ FILES ["insert_first"] ["type"]. "<br/> ";
Echo "file size:". ($ _ FILES ["insert_first"] ["size"]/1024). "Kb <br/> ";
Echo "temporary name:". $ _ FILES ["insert_first"] ["tmp_name"]. "<br/> ";
        }       
} Else {
Echo "the format of the uploaded file is incorrect ";
Exit;
}

// Connect to the database
$ Con = new mysql tutorial I ('localhost', 's503440db0', 'td9928ps ', 's503440db0 ');
If (mysqli_connect_errno ()){
Echo "failed to connect to database ";
Exit;
}
 
 
// Open the file
$ File = fopen ($ _ FILES ["insert_first"] ["tmp_name"], "r") or exit ("failed to open the file! ");

Var_dump ($ file );
Echo "<br/> ";

While (! Feof ($ file )){
// Split row by row
$ Row = explode (",", fgets ($ file ));
$ Row0 = $ row [0];
$ Row1 = (string) $ row [1];
Echo "$ row0". "---". "$ row1". "<br/> ";

// Determine whether a record exists
$ Query1 = "select firstid from firstcategory where firstid = $ row0 ";
$ Query2 = "select firstname from firstcategory where firstname = '$ row1 '";
$ Result1 = $ con-> query ($ query1 );
$ Result2 = $ con-> query ($ query2 );
// Determine the number of rows
$ Result1 = mysqli_num_rows ($ result1 );
$ Result2 = mysqli_num_rows ($ result2 );

                 
If ($ result1 ){
Echo "the row number {$ row0} already exists <br/> ";
} Elseif ($ result2 ){
Echo "category {$ row1} already exists <br/> ";
} Else {
$ Query = "insert into firstcategory (firstid, firstname) values ($ row0, 'Music ')";
$ Result = $ con-> query ($ query );
        }
}
Echo "import ended, please check if data is http://www.111cn.net correct ";


Fclose ($ file );
$ Con-> close ();

?>

Mysql database

"Create table firstcategory (
Firstid int unsigned not null primary key,
Firstname char (50) not null
)";

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.