首页 > 开发 > PHP > 正文

php 数据库内容以数组形式保存文件中

2024-05-04 21:58:26
字体:大 中 小
来源:转载
供稿:网友

这里把数据库中的常用的内容保存到一个标准的php格式的文件当中,这样使用起来也方便了很多,下面直接看代码.

  1. <?php 
  2. $res=mysql_query("select k1,k2  from ".table('keywords')." ") ; 
  3. $str="<?php rn "; 
  4. while($rs=mysql_fetch_array($res)) 
  5. { 
  6.  
  7.  $str .="$keyword['".$rs[0]."']='".$rs[1]."';rn"; 
  8. } 
  9. $str.="?>"; 
  10. file_put_contents("keyword.php",$str); 
  11. echo "导出成功"; 
  12.  
  13. //方法二 
  14.  
  15. $f=file_get_contents("w1.txt"); 
  16. $f=str_replace("rn","<br>",$f);//替换换行符 
  17. $arr=explode("<br>",$f); 
  18. $str="<?php rn "; 
  19. foreach($arr as $t) 
  20. { 
  21.  $rs=explode("|",$t); 
  22.  //不能包含?等特殊符号 
  23.  $str .="$keyword['".str_replace("?","",$rs[0])."']='".str_replace("?","",$rs[1])."';rn"; 
  24.  
  25. } 
  26. $str.="?>"; 
  27. file_put_contents("keyword.php",$str); 
  28. echo "导出成功"; 
  29.  
  30. //conn.php文件//开源代码Vevb.com 
  31. $conn=mysql_connect($mysql_host,$mysql_user,$mysql_password) or die('连接服务器出错'); 
  32. mysql_select_db($mysql_db) or die("选择数据库出错"); 
  33. mysql_query("set names 'gbk'"); 
  34. function table($t) 
  35. { 
  36.  global $mysql_table_prefix; 
  37.  return $mysql_table_prefix.$t; 
  38. } 
  39. ?> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表