Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-05-25 16:42:42
Original
871 people have browsed it

这里把数据库中的常用的内容保存到一个标准的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;
}
Copy after login


教程地址:

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

Related labels:
php
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 admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template