PHP processes SQL script files and imports them to the MySQL code instance _ PHP Tutorial

Source: Internet
Author: User
Tags mysql code mysql host
PHP processes the SQL script file and imports it to the MySQL code instance. Copy the code as follows :? PhpNameofthefile $ filenamechurch C. SQL; MySQLhost $ mysql_hostlocalhost; MySQLusername $ mysql_usernameroot; MySQLpassword The code is as follows:


// Name of the file
$ Filename = 'chilc. 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 each line
Foreach ($ lines as $ line)
{
// Skip it if it's a comment
If (substr ($ line, 0, 2) = '--' | $ line = '')
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 checking Ming query \''. $ Templine.' \ ':'. mysql_error ().'

');
// Reset temp variable to empty
$ Templine = '';
}
}
Echo "Tables imported successfully ";
?>

The http://www.bkjia.com/PHPjc/742450.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/742450.htmlTechArticle code is as follows :? Php // Name of the file $ filename = 'chilc. SQL '; // MySQL host $ mysql_host = 'localhost'; // MySQL username $ mysql_username = 'root'; // MySQL password...

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.