php mysql数据库导入脚本(示例)

WBOY
Release: 2016-07-25 08:55:09
Original
944 people have browsed it
  1. //php实现mysql数据导入
  2. define("DbHost", "localhost"); //数据库主机
  3. define("DbUser", "root"); //数据库用户
  4. define("DbPass", "password"); //数据库口令
  5. mysql_connect(DbHost,DbUser,DbPass) or die("不能连接服务器!");
  6. mysql_create_db("dbname") or die("不能建立数据库,可能您已经安装过了");
  7. mysql_select_db("dbname") or die("不能选择数据库,安装失败");
  8. $fp=fopen("install.sql","r") or die("不能打开SQL文件,请检查");
  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 "安装成功";
  14. ?>
复制代码

1,SQL文件中没有创建数据库语句时

  1. define("DbHost", "localhost"); //数据库主机
  2. define("DbUser", "root"); //数据库用户
  3. define("DbPass", "password"); //数据库口令
  4. define("DbName", "dbname"); //数据库名
  5. mysql_connect(DbHost,DbUser,DbPass) or die("不能连接服务器!");
  6. mysql_create_db(DbName) or die("不能建立数据库,可能您已经安装过了");
  7. mysql_select_db(DbName) or die("不能选择数据库,安装失败");
  8. $fp=fopen("install.sql","r") or die("不能打开SQL文件,请检查");
  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 "安装成功";
  14. ?>
复制代码

2,SQL文件中有创建数据库语句(即包含CREATE DATABASE和USE语句)时

  1. define("DbHost", "localhost"); //数据库主机
  2. define("DbUser", "root"); //数据库用户
  3. define("DbPass", "password"); //数据库口令
  4. mysql_connect(DbHost,DbUser,DbPass) or die("不能连接服务器!");
  5. $fp=fopen("install.sql","r") or die("不能打开SQL文件,请检查");
  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 "安装成功";
  11. ?>
复制代码


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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!