首頁  >  文章  >  php教程  >  php 数据库内容以数组形式保存文件中

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

WBOY
WBOY原創
2016-05-25 16:42:42832瀏覽

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

<?php
$res = mysql_query("select k1,k2  from " . table(&#39;keywords&#39;) . " ");
$str = "<?php \r\n ";
while ($rs = mysql_fetch_array($res)) {
    $str.= "$keyword[&#39;" . $rs[0] . "&#39;]=&#39;" . $rs[1] . "&#39;;\r\n";
}
$str.= "";
file_put_contents("keyword.php", $str);
echo "导出成功";
//方法二
$f = file_get_contents("w1.txt");
$f = str_replace("\r\n", "<br>", $f); //替换换行符
$arr = explode("<br>", $f);
$str = "<?php \r\n ";
foreach ($arr as $t) {
    $rs = explode("|", $t);
    //不能包含?等特殊符号
    $str.= "$keyword[&#39;" . str_replace("?", "", $rs[0]) . "&#39;]=&#39;" . str_replace("?", "", $rs[1]) . "&#39;;\r\n";
}
$str.= "";
file_put_contents("keyword.php", $str);
echo "导出成功";
//conn.php文件
$conn = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(&#39;连接服务器出错&#39;);
mysql_select_db($mysql_db) or die("选择数据库出错");
mysql_query("set names &#39;gbk&#39;");
function table($t) {
    global $mysql_table_prefix;
    return $mysql_table_prefix . $t;
}


教程地址:

欢迎转载!但请带上文章地址^^

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn