PHP executes the SQL file and imports the SQL file into the database, _php tutorial

Source: Internet
Author: User

PHP executes the SQL file and imports the SQL file into the database.


Read file contents $_sql = file_get_contents (' Test.sql '); $_arr = explode ('; ', $_sql); $_mysqli = new Mysqli (db_host,db_user,db_ PASS), if (Mysqli_connect_errno ()) {  exit (' Error connecting database ');} Execute SQL statement foreach ($_arr as $_value) {  $_mysqli->query ($_value. '; ');} $_mysqli->close (); $_mysqli = null;

Above text.sql is the SQL file you need to execute, db_host hostname, db_user username, db_pass password!

This is just the most basic automated SQL file, you can also customize the name of the build database by deleting the following code in the SQL file

CREATE database IF not EXISTS db name DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;

Use database name

Then add the code before executing all the SQL statements in text.php

$_mysqli->query ("CREATE database IF not EXISTS the name DEFAULT CHARACTER SET UTF8 COLLATE utf8_general_ci;"); $_mysqli->query ("Use database name");

The above is the whole content of this article, I hope to help you.

http://www.bkjia.com/PHPjc/1057091.html www.bkjia.com true http://www.bkjia.com/PHPjc/1057091.html techarticle PHP executes the SQL file and imports the SQL file into the database,//reads the file contents $_sql = file_get_contents (' Test.sql '); $_arr = explode ('; ', $_sql); $_mysqli = new Mysqli (db_host,db_user,d ...

  • 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.