PHP配置数据库XML文件
<?php $doc=new DOMDocument('1.0','utf-8'); //new一个dom对象 $doc->load("config.xml"); 加载xml文件 $mysql=$doc->getElementsByTagName("mysql"); //获取xml为mysql的节点 $mysql1=$mysql->item(0); //第一个 $servicer=$mysql1->getElementsByTagName("servicer")->item(0)->nodeValue; //获取服务器地址 echo $servicer; $root=$mysql1->getElementsByTagName("root")->item(0)->nodeValue; //获取用户名 echo $root; $pw=$mysql1->getElementsByTagName("pw")->item(0)->nodeValue; //获取密码 echo $pw; echo "<hr>"; $link=mysql_connect("$servicer","$root","$pw")or die('连接数据库失败'); mysql_select_db('laravelbook',$link) or die('失败'); $sql='select *from posts;'; $res=mysql_query($sql); while($rows=mysql_fetch_row($res)){ foreach($rows as $value) {echo $value;} } XML文件 <?xml version="1.0" encoding="utf-8"?> <mysql_parameter> <mysql> <servicer>localhost</servicer> <root>root</root> <pw>ok</pw> </mysql> </mysql_parameter>