Home > Backend Development > PHP Tutorial > php mysql database import script (example)

php mysql database import script (example)

WBOY
Release: 2016-07-25 08:55:09
Original
1071 people have browsed it
  1. //php implements 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 the server!");
  6. mysql_create_db("dbname") or die("Cannot create database, maybe You have already installed it");
  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

1, when there is no database statement created in the 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 the server!");
  6. mysql_create_db(DbName) or die ("Cannot create database, you may have already installed it");
  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

2, the database is created in the SQL file statement (i.e. containing CREATE DATABASE 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 the 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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template