怎么生成config.php这样的数据库配置信息文件

WBOY
Release: 2016-06-13 13:32:11
Original
1185 people have browsed it

如何生成config.php这样的数据库配置信息文件
我刚学习PHP,写一个创建数据库并创建表单的PHP后,想把用户填写的数据库地址,数据库用户名,数据库密码,创建的数据库名称等信息创建一个config.php保存到里面供以后操作数据库使用,应当怎么写呢?
下面是我获取表单中用户输入数据创建数据库的代码

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $sqladdress = $_POST["sqladdress"];
$sqluser = $_POST["sqluser"];
$sqlpassword = $_POST["sqlpassword"];
$sqlname = $_POST["sqlname"];
$con = mysql_connect("$sqladdress", "$sqluser", "$sqlpassword");
if (!$con)
{
    die(mysql_errno());
}
if (mysql_query("CREATE DATABASE $sqlname", $con))
{
    echo "database created";
}
else
{
    echo "database creat error" . mysql_errno();
}
?>

Copy after login


------解决方案--------------------
$fp = fopen('config.php', 'w+');
$config = "configure: \r\n";
$config .= ' $DB[\'address\'] = '. $sqladdress ."\r\n";
// ...类似这样的
fwrite($fp, $config);
fclose($fp);

然后读取该配置文件时:
include 'config.php';
mysql_connect($DB['address'], ...)
------解决方案--------------------
放数据库里就行了...

放文件里也一样,json或者csv格式放进去就可以了,下次也方便解析。
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!