PHP Import SQL to MySQL database method parsing

Source: Internet
Author: User
Tags php mysql
Copy Code

------Answer--------The second one has not been done, follow this issue. The third problem: The simplest, you can execute the mysqldump command.

    1. $database = "";//database name backed up
    2. $user = "";//database connection user name
    3. $password = "";//Database Password
    4. $outputfile = "";//The SQL file path of the exported backup
    5. System ($mysqldump. "-U". $user. "-P". $password. " ". $database." > ". $outputfile.");
    6. ?>
Copy Code

------answer--------Your problems with the MySQL client software can be implemented, using MySQL cc or MySQL front can achieve your requirements, the use of the program will be very troublesome------answer--------to do it. or Google PHP MySQL class adobd lite------answer--------Good------answer--------for 1,3 can use the command line that comes with MySQL to complete the import and export work. Assume that the MySQL user name and password are root xxx, the database is test import SQL MYSQL-HLOCALHOST-UROOT-PXXX Test < XXX.SQL export SQL Mysqldump-hlocalhost-uroo T-pxxx Database > Xxx.sql for Excel, standard data can be converted to CVS-formatted data, and then imported with tools such as phpmyadmin------answer--------two ways: 1. Execute the mysql command directly, Let MySQL read the file 2. Read the file in PHP and execute the file contents. ------Solution--------1,3 is simple 2. Basically in the Chinese environment there is no perfect solution can use the DOM to write a small program to do the conversion------solution--------is com to =.=| | ------Answer--------The second question, you can write a CSV file to import the data can be imported into the MySQL database------solution--------Excel out of the kind of csv ... =_=| | The data is easy to use------answer--------support under 1. 2 Floor 2 question can consider MySQL ODBC------answer--------

Reference 7 Floor Froole reply: Two methods: 1. Execute the mysql command directly, let MySQL read the corresponding file 2. read the file in PHP and execute the file contents.

Simple and concise expression------solution--------The above points are very high, sweat. ------Answer--------The second question is this, in order to batch import the database some files, before ASP is done by ASP to the. xls file read out, write into the database. Now to PHP, plus you are completely a novice, want to do is, the collation of the. xls file through PHP directly into the MySQL, is not so bad ah. What is the best way to achieve bulk import? For example, to import the names of all students in a school. ------Answer--------gingzai777 Master method is trying, is not the address behind the source must be an absolute address it? ------Answers--------

Reference 1 Floor gingzai777 's reply: 1, import the existing SQL file into the database, I write a function:

    1. $conn =mysql_connect ("localhost", "root", "password");//Specify Database connection parameters
    2. function Mysql_import ($file, $database)//Imported functions, parameters are SQL file path and imported library name.
    3. {
    4. mysql_select_db ($database);
    5. mysql_query ("source". $file. "';");
    6. echo "Import". $file. " File to ". $database." Database Complete ";
    7. }
    8. Mysql_close ($conn);
    9. ?>
Copy Code

+---------Code---------------+

    1. function init_db () {
    2. $file = ": /install/import.sql ";
    3. $conn =mysql_connect ($this->dbhost, $this->dbuser, $this->dbpw) or Die ("Database connection Error");
    4. if (! $conn) {
    5. echo "Database connection error";
    6. }
    7. $db =mysql_select_db ($this->dbname);
    8. if ($db) {
    9. mysql_query ("DROP DATABASE". $this->dbname. " `");
    10. }
    11. mysql_query ("Setnames ' GBK '");
    12. $creatdb =mysql_query ("CREATE DATABASE". $this->dbname. " ' DEFAULT CHARACTER set gb2312 COLLATE gb2312_bin ');//Establish database
    13. $mysql =mysql_select_db ($this->dbname);
    14. //if ($mysql) {
    15. //echo "Connection";
    16. //}else{
    17. //echo "Not Connected";
    18. //}
    19. //echo $file;
    20. //$file = "source". $file. "'";
    21. //echo $file;
    22. mysql_query ("source". $file. ""); /Initialize Database
    23. mysql_close ($conn);
    24. }
Copy Code

------Answer--------1, import the existing SQL file into the database, I write a function:

    1. $conn =mysql_connect ("localhost", "root", "password");//Specify Database connection parameters
    2. function Mysql_import ($file, $database)//Imported functions, parameters are SQL file path and imported library name.
    3. {
    4. mysql_select_db ($database);
    5. mysql_query ("source". $file. "';");
    6. echo "Import". $file. " File to ". $database." Database Complete ";
    7. }
    8. Mysql_close ($conn);
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.