PHP processing SQL script file import to MySQL code instance

Source: Internet
Author: User
Tags mysql code mysql host mysql database

  usually in the production of the installer, data Backup program will use this code, I think there is not much online, and some are not very good, sometimes this code directly with ready-made can save a lot of time, then I transferred from the StackOverflow a come, Need friends can refer to the following

The code is as follows: <?php  //Name of the file $filename = ' churc.sql '; MySQL host $mysql _host = ' localhost '; MySQL username $mysql _username = ' root '; MySQL password $mysql _password = '; Database name $mysql _database = ' dump ';  //Connect to MySQL server mysql_connect ($mysql _host, $mysql _username, $mysql _password) or Die (' Error connecting to MySQL server: '. Mysql_error ()); Select database mysql_select_db ($mysql _database) or Die (' Error selecting MySQL database: '. mysql_error ());  //temporary variable, used to store current query $templine = '; Read in entire file $lines = file ($filename); Loop through per line foreach ($lines as $line) {//Skip it if it ' s a comment if (substr ($line, 0, 2) = '--' | | $lin E = = ')     continue;  //ADD This line to the current segment $templine. = $line; If it has a semicolon at the end, it's the end of the query if (substr (Trim ($line),-1, 1) = = '; ') {   //Perform the query     mysql_query ($templine) or print (' Error performing query ' <strong> '. $templine. '': ' . Mysql_error (). ' <br/><br/> ');    //Reset temp variable to empty     $templine = '; }  echo "Tables imported successfully";?>
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.