Use php to execute the SQL file and import the SQL file to the database,

Source: Internet
Author: User

Use php to execute the SQL file and import the SQL file to the database,

Today, we will introduce how to use php to automatically execute. SQL files. In fact, it is very easy to get the content in the SQL file, and then execute each SQL statement once.

This is the code


// Read the file content $ _ SQL = file_get_contents ('test. SQL '); $ _ arr = explode ('; ', $ _ SQL); $ _ mysqli = new mysqli (DB_HOST, DB_USER, DB_PASS); if (mysqli_connect_errno ()) {exit ('database connection error ');} // execute the SQL statement foreach ($ _ arr as $ _ value) {$ _ mysqli-> query ($ _ value. ';') ;}$ _ mysqli-> close (); $ _ mysqli = null;

 

Text. SQL above is the SQL file you need to execute, DB_HOST host name, DB_USER username, DB_PASS password!

This is just the most basic Automatic Execution of SQL files. You can alsoName of the custom generated databaseTo delete the code in the SQL File

12
Create database if not exists database name default character set utf8 COLLATE utf8_general_ci; use database Name

 

Add the code before executing all SQL statements in text. php.

$ _ Mysqli-> query ("create database if not exists database name default character set utf8 COLLATE utf8_general_ci;"); $ _ mysqli-> query ("use database Name ");

 


Replace the database name with the name you need to modify! You can explore more features on your own!

Reprinted please indicate the source: http://www.ly89.cn/detailB/35.html

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.