当前位置:编程学习 > php >>

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

php教程 数据库教程内容以数组形式保存文件中
这里把数据库中的常用的内容保存到一个标准的php格式的文件当中,这样使用起来也方便了很多,下面代码。
*/
$res=mysql教程_query("select k1,k2  from ".table('keywords')." ") ;
$str="<?php rn ";
while($rs=mysql_fetch_array($res))
{
 
 $str .="$keyword['".$rs[0]."']='".$rs[1]."';rn";
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";

//方法二

$f=file_get_contents("w1.txt");
$f=str_replace("rn","<br>",$f);//替换换行符
$arr=explode("<br>",$f);
$str="<?php rn ";
foreach($arr as $t)
{
 $rs=explode("|",$t);
 //不能包含?等特殊符号
 $str .="$keyword['".str_replace("?","",$rs[0])."']='".str_replace("?","",$rs[1])."';rn";
 
}
$str.="?>";
file_put_contents("keyword.php",$str);
echo "导出成功";

//conn.php文件
$conn=mysql_connect($mysql_host,$mysql_user,$mysql_password) or die('连接服务器出错');
mysql_select_db($mysql_db) or die("选择数据库出错");
mysql_query("set names 'gbk'");
function table($t)
{
 global $mysql_table_prefix;
 return $mysql_table_prefix.$t;
}
?>

补充:Php教程,Php常用代码 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,