PHP MySQL Database import script (example)

Source: Internet
Author: User
Tags fread php mysql
    1. PHP implementation MySQL data import
    2. Define ("Dbhost", "localhost"); Database Host
    3. Define ("DbUser", "root"); Database user
    4. Define ("Dbpass", "password"); Database Password
    5. Mysql_connect (Dbhost,dbuser,dbpass) or Die ("Cannot connect to server!");
    6. mysql_create_db ("dbname") or Die ("could not establish database, you may have installed");
    7. mysql_select_db ("dbname") or Die ("Cannot select database, installation Failed");
    8. $FP =fopen ("Install.sql", "R") or Die ("Cannot open SQL file, please check");
    9. $sql =fread ($fp, FileSize ("Install.sql"));
    10. Fclose ($FP);
    11. $sql =explode (";", $sql);
    12. for ($i =0; $i mysql_query ($sql [$i]);
    13. echo "Installation Successful";
    14. ?>
Copy Code

When a database statement is not created in the 1,sql file

    1. Define ("Dbhost", "localhost"); Database Host
    2. Define ("DbUser", "root"); Database user
    3. Define ("Dbpass", "password"); Database Password
    4. Define ("DbName", "DbName"); Database name
    5. Mysql_connect (Dbhost,dbuser,dbpass) or Die ("Cannot connect to server!");
    6. mysql_create_db (DbName) or die ("could not establish database, you may have installed");
    7. mysql_select_db (DbName) or Die ("Cannot select database, installation Failed");
    8. $FP =fopen ("Install.sql", "R") or Die ("Cannot open SQL file, please check");
    9. $sql =fread ($fp, FileSize ("Install.sql"));
    10. Fclose ($FP);
    11. $sql =explode (";", $sql);
    12. for ($i =0; $i mysql_query ($sql [$i]);
    13. echo "Installation Successful";
    14. ?>
Copy Code

The 2,sql file has the CREATE DATABASE statement (that is, contains the Create databases and use statements)

    1. Define ("Dbhost", "localhost"); Database Host
    2. Define ("DbUser", "root"); Database user
    3. Define ("Dbpass", "password"); Database Password
    4. Mysql_connect (Dbhost,dbuser,dbpass) or Die ("Cannot connect to server!");
    5. $FP =fopen ("Install.sql", "R") or Die ("Cannot open SQL file, please check");
    6. $sql =fread ($fp, FileSize ("Install.sql"));
    7. Fclose ($FP);
    8. $sql =explode (";", $sql);
    9. for ($i =0; $i mysql_query ($sql [$i]);
    10. echo "Installation Successful";
    11. ?>
Copy Code
  • 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.