Php SQL import to mysql database method analysis

Source: Internet
Author: User
Tags php mysql
Php SQL import to mysql database method analysis

------ Answer -------- The second question has not been done yet. pay attention to this question. The third problem: the simplest one is to execute the mysqldump command.

  1. $ Database = ""; // name of the backup database
  2. $ User = ""; // database connection username
  3. $ Password = ""; // database password
  4. $ Outputfile = ""; // export the SQL file path of the backup
  5. System ($ mysqldump. "-u". $ user. "-p". $ password. "". $ database. ">". $ outputfile ."");
  6. ?>

------ Answer -------- you can use mysql client software to solve these problems, and use mysql cc or mysql front to meet your requirements, it will be very troublesome to use the program ------ answer -------- The execution is not enough or google php mysql class adobd lite ------ answer -------- good ------ answer -------- for 1 and 3, you can use the command line provided by mysql to complete the import and export work. assume that the mysql User name and password are root xxx, and the database is test to import SQL mysql-hlocalhost-uroot-pxxx test <xxx. SQL export SQL mysqldump-hlocalhost-uroot-pxxx database> xxx. SQL for excel, the standard data can be converted into cvs data, and then use phpmyadmin Tool import ------ answer -------- two methods: 1. directly execute the mysql command to let Mysql read the corresponding file 2. read the file with PHP, and then execute the file content. ------ Answer -------- 1, 3 is very simple 2. basically, there is no perfect solution in the Chinese environment. you can use DOM to write a small program for conversion ------ the answer -------- is COM. = | ------ answer -------- The second question: you can write a csv file to import the data to the mysql database. ------ answer -------- The csv file in excel... = _ = | the data is simple and can be used for a moment ------ answer -------- support 1. For questions on the second floor, consider mysql odbc ------ answer --------

Reference the response from froole on the 7th floor: 1. execute the Mysql command directly to allow Mysql to read the corresponding file. 2. use PHP to read the file and then execute the file content.

Concise and clear expression ------ answer -------- the above points are high and sweaty. The hosts file is read and written into the database. Changing to PHP is completely new. what you want to do is to import the organized. xls file directly to mysql through PHP. isn't that all possible. What are the best methods for batch import? For example, you need to import the student ID of all the students in a school. ------ Answer -------- The gingzai777 expert's method is being tested. Is it true that the address after source must be an absolute address? ------ Answer --------

Reference gingzai777 on the first floor: 1. import the existing SQL file to the database. I will write a function:

  1. $ Conn = mysql_connect ("localhost", "root", "password"); // specify the database connection parameters
  2. Function mysql_import ($ file, $ database) // The Imported function. the parameter is the SQL file path and the imported database name.
  3. {
  4. Mysql_select_db ($ database );
  5. Mysql_query ("source". $ file ."';");
  6. Echo "import". $ file. "file to". $ database. "database completed ";
  7. }
  8. Mysql_close ($ conn );
  9. ?>

+ --------- 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"); // CREATE a DATABASE
  13. $ Mysql = mysql_select_db ($ this-> dbname );
  14. // If ($ mysql ){
  15. // Echo "connect ";
  16. //} Else {
  17. // Echo "not connected ";
  18. //}
  19. // Echo $ file;
  20. // $ File = "source". $ file ."'";
  21. // Echo $ file;
  22. Mysql_query ("source". $ file. "'"); // initialize the database
  23. Mysql_close ($ conn );
  24. }

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

  1. $ Conn = mysql_connect ("localhost", "root", "password"); // specify the database connection parameters
  2. Function mysql_import ($ file, $ database) // The Imported function. the parameter is the SQL file path and the imported database name.
  3. {
  4. Mysql_select_db ($ database );
  5. Mysql_query ("source". $ file ."';");
  6. Echo "import". $ file. "file to". $ database. "database completed ";
  7. }
  8. Mysql_close ($ conn );

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.