Home  >  Article  >  Backend Development  >  php import SQL file (sample code)

php import SQL file (sample code)

WBOY
WBOYOriginal
2016-07-25 08:54:561221browse
  1. /************

  2. *
  3. PHP import .sql file
  4. Running version: php5, php4
  5. Author: panxp
  6. E-mail: coolpan123@gmail.com
  7. *Edited and organized: bbs.it-home.org
  8. *
  9. *************/
  10. $host = "localhost";
  11. $user = "root";
  12. $pwd = "";
  13. $file_dir = dirname(__FILE__);
  14. $file_name = "bar.sql";
  15. $data_base = "test";

  16. $conn = mysql_connect($host,$user,$pwd);

  17. mysql_select_db($data_base,$conn);

  18. /**PHP5 version **/
  19. $get_sql_data = file_get_contents($file_name,$file_dir);

  20. /**

  21. * PHP4 版本
  22. if(file_exists($file_dir."/".$file_name))
  23. {
  24. $get_sql_data = fopen($file_dir."/".$file_name,"r");
  25. if(!$get_sql_data)
  26. {
  27. echo "不能打开文件";
  28. }
  29. else
  30. {
  31. $get_sql_data = fread($get_sql_data, filesize ($file_dir."/".$file_name));
  32. }
  33. }
  34. ***/
  35. $explode = explode(";",$get_sql_data);
  36. $cnt = count($explode);
  37. for($i=0;$i
  38. $sql = $explode[$i];

  39. $result = mysql_query($sql);

  40. if($result){

  41. echo "成功:".$i."个查询
    ";
  42. }
  43. else
  44. {
  45. echo "导入失败:".mysql_error();
  46. }
  47. }
  48. ?>

复制代码


Statement:
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