应用XML作为项目的配置文件使用,并解析之,获得数据作为链接数据库的参数
使用XML作为项目的配置文件使用,并解析之,获得数据作为链接数据库的参数
db.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
<db>
<host>
localhost
</host>
<dbname>
ecshop
</dbname>
<user>
root
</user>
<pass>
123
</pass>
</db>
<smarty></smarty>
</config>
db.php
<?php
$doc = new DOMDocument('1.0','utf-8');
$doc ->load('db.xml');
$db = $doc ->getElementByTagName('db')->item(0);
$smarty = $doc ->getElementByTagName('smarty')->item(0) ->nodeValue;
$host = $db ->getElementByTagName('host')->item(0)->nodeValue;
$user = $db ->getElementByTagName('user')->item(0)->nodeValue;
$pass = $db ->getElementByTagName('pass')->item(0)->nodeValue;
$dbname = $db ->getElementByTagName('dbname')->item(0)->nodeValue;
mysql_connect($host,$user,$pass);
mysql_select_db($dbname);
?>
db.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
<db>
<host>
localhost
</host>
<dbname>
ecshop
</dbname>
<user>
root
</user>
<pass>
123
</pass>
</db>
<smarty></smarty>
</config>
db.php
<?php
$doc = new DOMDocument('1.0','utf-8');
$doc ->load('db.xml');
$db = $doc ->getElementByTagName('db')->item(0);
$smarty = $doc ->getElementByTagName('smarty')->item(0) ->nodeValue;
$host = $db ->getElementByTagName('host')->item(0)->nodeValue;
$user = $db ->getElementByTagName('user')->item(0)->nodeValue;
$pass = $db ->getElementByTagName('pass')->item(0)->nodeValue;
$dbname = $db ->getElementByTagName('dbname')->item(0)->nodeValue;
mysql_connect($host,$user,$pass);
mysql_select_db($dbname);
?>