目录结构
chatroom文件下有source文件夹、conf文件夹、userlist.php等
source文件夹下有SqlHelper.class.php、userlistController.php
conf文件夹下有dbconf.xml.
其他文件忽略。
现在问题是当userlist.php不在source文件夹下时会出现如下提示。(放在source文件夹下正常》
Warning: DOMDocument::load() [function.DOMDocument-load]: I/O warning : failed to load external entity "file:///C%3A/wamp/www/test/ajax/chatroomtest/conf/dbconf.xml" in C:\wamp\www\test\ajax\chatroomtest\chatroom\source\SqlHelper.class.php on line 14
Fatal error: Call to a member function getElementsByTagName() on a non-object in C:\wamp\www\test\ajax\chatroomtest\chatroom\source\SqlHelper.class.php on line 19
详细代码楼下贴
3 个解决方案
#1
userlistController.php代码
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
include 'SqlHelper.class.php';
function getlist($listtype,$loginname){
$userlist="";
$name=$loginname;
switch($listtype){
case 'a':
$userlist="";
$sql="select adminname from adminlist where isLog=0 adminname!='$name' order by rand() limit 15";
$sqlHelper=new SqlHelper();
$array=$sqlHelper->execute_dql1($sql);
for($i=0;$i<count($array);$i++){
$array[$i];
$res=$array[$i][0];
$userlist.="<li><img src='";
$userlist.="#' /><a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)' >".$res."</a></li>";
}
$sqlHelper->close_connect();
break;
case 'u':
$userlist="";
//随机读取15个用户,数据量过大时不可用此。
$sql1="select username from users where isLog=0 and username!='$name' order by rand() limit 15";
$sqlHelper=new SqlHelper();
$array1=$sqlHelper->execute_dql1($sql1);
for($i=0;$i<count($array1);$i++){
$array[$i];
$res=$array1[$i][0];
$userlist.="<li><img src='img/common/none.gif' />";
$userlist.="<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)' >".$res."</a></li>";
}
$sqlHelper->close_connect();
break;
case 'f':
$userlist="";
$sql="select u.username from (select id from users where username='$name') a, friends f,users u where u.isLog=0 and f.userid=a.id";
$sqlHelper=new SqlHelper();
$array=$sqlHelper->execute_dql1($sql);
for($i=0;$i<count($array);$i++){
$array[$i];
$res=$array[$i][0];
$userlist.="<li><img src='#' /><a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)' >".$res."</a></li>";
}
$sqlHelper->close_connect();
break;
default:
break;
}
echo $userlist;
}
?>
#2
SqlHelper.class.php代码
<?php
class SqlHelper {
public $conn;
public $dbname;
public $username;
public $password;
public $host;
public function __construct(){
$xmldoc=new DOMDocument();
//加载xml文件
$xmldoc->load("../conf/dbconf.xml");
//获取元素
$dbinfo=$xmldoc->getElementsByTagName("dbinfo");
$dbinfo1=$dbinfo->item(0);
//获取节点值
$this->host=$dbinfo1->getElementsByTagName("host")->item(0)->nodeValue;
$this->dbname=$dbinfo1->getElementsByTagName("dbname")->item(0)->nodeValue;
$this->username=$dbinfo1->getElementsByTagName("username")->item(0)->nodeValue;
$this->password=$dbinfo1->getElementsByTagName("password")->item(0)->nodeValue;
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn){
die("连接失败".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
}
//执行dql语句 查询
public function execute_dql2($sql){
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_error());
//$i=0;关联数组方式取,只能用字段名称
while($row=mysql_fetch_assoc($res)){
//$arr[$i++]=$row;
$arr[]=$row;
}
mysql_free_result($res);
return $arr;
}
public function execute_dql1($sql){
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_error());
//$i=0;
while($row=mysql_fetch_row($res)){
$arr[]=$row;
}
mysql_free_result($res);
return $arr;
}
//考虑分页情况的查询,拓展功能在FenyePage.class.php中添加属性即可.
//$sql1="select * from where 表名 limit 0,6";
//$sql2="select count(id) from 表名"
public function exectue_dql_fenye($sql1,$sql2,$fenyePage){
//查询要分页显示的数据
$res=mysql_query($sql1,$this->conn) or die(mysql_error());
$arr=array();
while($row=mysql_fetch_assoc($res)){
$arr[]=$row;
}
mysql_free_result($res);
$res2=mysql_query($sql2,$this->conn) or die(mysql_error());
if($row=mysql_fetch_row($res2)){
$fenyePage->pageCount=ceil($row[0]/$fenyePage->pageSize);
$fenyePage->rowCount=$row[0];
}
mysql_free_result($res2);
//分页导航封装
$fenyeNav="";
if ($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$fenyeNav="<a href='{$fenyePage->gotoUrl}?pageNow=$prePage'>上一页</a>";
}
if ($fenyePage->pageNow<$fenyePage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=$nextPage'>下一页</a>";
}
$page_whole=10;//页数序号数
$start=floor(($fenyePage->pageNow-1)/$page_whole)*$page_whole+1;
$index=$start;
if($fenyePage->pageNow>$page_whole){
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=".($start-1)."'><<</a>";
}
for($start;$start<$index+$page_whole;$start++){
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'>[$start]</a>";
}
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'>>></a>";
$fenyeNav.="当前页{$fenyePage->pageNow}/共{$fenyePage->pageCount}页";
$fenyePage->res_array=$arr;
$fenyePage->fenyeNav=$fenyeNav;
}
/*
//封装导航信息
$navigate="";
if ($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$navigate="<a href='empList.php?pageNow=$prePage'>上一页</a>"
}
if($fenyePage->pageNow<$fenyePage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$navigate.="<a href='empList.php?pageNow=$nextPage'>下一页</a>"
//把数组赋给实例
$fenyePage->res_array=$arr;
$fenyePage->navigate=$navigate;
}
*/
//执行dml语句添加
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn) or die(mysql_error());
if(!$b){
return 0;
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示执行成功
}else{
return 2;//表示行无影响
}
}
}
//关闭连接的方法
public function close_connect(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>
#3
userlist.php代码
<?php
include 'source/userlistController.php';
session_start();
$loginuser=$_SESSION['username'];
if(!$loginuser){
ob_start();
header("Location:login.php?errno=3");
ob_clean();
exit();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/userlist.css"/>
<script type="text/javascript" language="javascript" src="js/base.js"></script>
<script type="text/javascript" language="javascript">
function moveOver(obj){
obj.style.backgroundColor="#FFFF00";
obj.style.cursor="pointer";
}
function openChatRoom(obj){
var url="../chatroom.php?username="+encodeURI(obj.innerText)+"";
var name="";
var iWidth=500;
var iHeight=550;
var iTop = (window.screen.availHeight-30-iHeight)/2;
var iLeft = (window.screen.availWidth-10-iWidth)/2;
window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no');
}
function change(val,obj){
obj.style.cursor="pointer";
obj.style.backgroundColor="#FFFF00";
if(val=='user'){
list_user.style.display='block';
list_admin.style.display='none';
list_firend.style.display='none';
}else if(val=='admin'){
list_admin.style.display='block';
list_user.style.display='none';
list_firend.style.display='none';
}else if(val=='friend'){
list_firend.style.display='block';
list_user.style.display='none';
list_admin.style.display='none';
}
}
function moveOut(obj){
obj.style.cursor="default";
obj.style.backgroundColor="#CCCCCC";
}
</script>
<title>好友列表</title>
</head>
<body>
<div class="main">
<!--头部-->
<div class="top">
</div>
<!--用户列表-->
<div class="list">
<div class="list_menu">
<ul>
<li onMouseOver="change('admin',this)" onMouseOut="moveOut(this)"><img src="img/common/adminlist.png" /></li>
<li onMouseOver="change('user',this)" onMouseOut="moveOut(this)"><img src="img/common/userlist.png" /></li>
<li onMouseOver="change('friend',this)" onMouseOut="moveOut(this)"><img src="img/common/friendlist.png" /></li>
</ul>
</div>
<div id="list_admin" class="list_admin">
<ul>
<li>
<img src="img/user/face/jimmy.gif" />
<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)'>jimmy</a>
</li>
<li>
<img src="img/user/face/zarte.gif" />
<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)'>zarte</a>
</li>
<li>
<img src="img/common/none.gif" />
<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)'>暗流</a>
</li>
</ul>
<!--分页-->
<div class="fengye">
</div>
</div>
<div id="list_user" class="list_user">
<ul>
<?php getlist('u',$loginuser);?>
</ul>
<!--分页-->
<div class="fengye">
</div>
</div>
<div id="list_firend" class="list_firend">
<ul>
<?php getlist('f',$loginuser); ?>
</ul>
<!--分页-->
<div class="fengye">
</div>
</div>
</div>
<!--底部-->
<div class="bottom">
</div>
</div>
</body>
</html>
#1
userlistController.php代码
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
include 'SqlHelper.class.php';
function getlist($listtype,$loginname){
$userlist="";
$name=$loginname;
switch($listtype){
case 'a':
$userlist="";
$sql="select adminname from adminlist where isLog=0 adminname!='$name' order by rand() limit 15";
$sqlHelper=new SqlHelper();
$array=$sqlHelper->execute_dql1($sql);
for($i=0;$i<count($array);$i++){
$array[$i];
$res=$array[$i][0];
$userlist.="<li><img src='";
$userlist.="#' /><a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)' >".$res."</a></li>";
}
$sqlHelper->close_connect();
break;
case 'u':
$userlist="";
//随机读取15个用户,数据量过大时不可用此。
$sql1="select username from users where isLog=0 and username!='$name' order by rand() limit 15";
$sqlHelper=new SqlHelper();
$array1=$sqlHelper->execute_dql1($sql1);
for($i=0;$i<count($array1);$i++){
$array[$i];
$res=$array1[$i][0];
$userlist.="<li><img src='img/common/none.gif' />";
$userlist.="<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)' >".$res."</a></li>";
}
$sqlHelper->close_connect();
break;
case 'f':
$userlist="";
$sql="select u.username from (select id from users where username='$name') a, friends f,users u where u.isLog=0 and f.userid=a.id";
$sqlHelper=new SqlHelper();
$array=$sqlHelper->execute_dql1($sql);
for($i=0;$i<count($array);$i++){
$array[$i];
$res=$array[$i][0];
$userlist.="<li><img src='#' /><a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)' >".$res."</a></li>";
}
$sqlHelper->close_connect();
break;
default:
break;
}
echo $userlist;
}
?>
#2
SqlHelper.class.php代码
<?php
class SqlHelper {
public $conn;
public $dbname;
public $username;
public $password;
public $host;
public function __construct(){
$xmldoc=new DOMDocument();
//加载xml文件
$xmldoc->load("../conf/dbconf.xml");
//获取元素
$dbinfo=$xmldoc->getElementsByTagName("dbinfo");
$dbinfo1=$dbinfo->item(0);
//获取节点值
$this->host=$dbinfo1->getElementsByTagName("host")->item(0)->nodeValue;
$this->dbname=$dbinfo1->getElementsByTagName("dbname")->item(0)->nodeValue;
$this->username=$dbinfo1->getElementsByTagName("username")->item(0)->nodeValue;
$this->password=$dbinfo1->getElementsByTagName("password")->item(0)->nodeValue;
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn){
die("连接失败".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
}
//执行dql语句 查询
public function execute_dql2($sql){
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_error());
//$i=0;关联数组方式取,只能用字段名称
while($row=mysql_fetch_assoc($res)){
//$arr[$i++]=$row;
$arr[]=$row;
}
mysql_free_result($res);
return $arr;
}
public function execute_dql1($sql){
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_error());
//$i=0;
while($row=mysql_fetch_row($res)){
$arr[]=$row;
}
mysql_free_result($res);
return $arr;
}
//考虑分页情况的查询,拓展功能在FenyePage.class.php中添加属性即可.
//$sql1="select * from where 表名 limit 0,6";
//$sql2="select count(id) from 表名"
public function exectue_dql_fenye($sql1,$sql2,$fenyePage){
//查询要分页显示的数据
$res=mysql_query($sql1,$this->conn) or die(mysql_error());
$arr=array();
while($row=mysql_fetch_assoc($res)){
$arr[]=$row;
}
mysql_free_result($res);
$res2=mysql_query($sql2,$this->conn) or die(mysql_error());
if($row=mysql_fetch_row($res2)){
$fenyePage->pageCount=ceil($row[0]/$fenyePage->pageSize);
$fenyePage->rowCount=$row[0];
}
mysql_free_result($res2);
//分页导航封装
$fenyeNav="";
if ($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$fenyeNav="<a href='{$fenyePage->gotoUrl}?pageNow=$prePage'>上一页</a>";
}
if ($fenyePage->pageNow<$fenyePage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=$nextPage'>下一页</a>";
}
$page_whole=10;//页数序号数
$start=floor(($fenyePage->pageNow-1)/$page_whole)*$page_whole+1;
$index=$start;
if($fenyePage->pageNow>$page_whole){
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=".($start-1)."'><<</a>";
}
for($start;$start<$index+$page_whole;$start++){
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'>[$start]</a>";
}
$fenyeNav.="<a href='{$fenyePage->gotoUrl}?pageNow=$start'>>></a>";
$fenyeNav.="当前页{$fenyePage->pageNow}/共{$fenyePage->pageCount}页";
$fenyePage->res_array=$arr;
$fenyePage->fenyeNav=$fenyeNav;
}
/*
//封装导航信息
$navigate="";
if ($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$navigate="<a href='empList.php?pageNow=$prePage'>上一页</a>"
}
if($fenyePage->pageNow<$fenyePage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$navigate.="<a href='empList.php?pageNow=$nextPage'>下一页</a>"
//把数组赋给实例
$fenyePage->res_array=$arr;
$fenyePage->navigate=$navigate;
}
*/
//执行dml语句添加
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn) or die(mysql_error());
if(!$b){
return 0;
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示执行成功
}else{
return 2;//表示行无影响
}
}
}
//关闭连接的方法
public function close_connect(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>
#3
userlist.php代码
<?php
include 'source/userlistController.php';
session_start();
$loginuser=$_SESSION['username'];
if(!$loginuser){
ob_start();
header("Location:login.php?errno=3");
ob_clean();
exit();
}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/userlist.css"/>
<script type="text/javascript" language="javascript" src="js/base.js"></script>
<script type="text/javascript" language="javascript">
function moveOver(obj){
obj.style.backgroundColor="#FFFF00";
obj.style.cursor="pointer";
}
function openChatRoom(obj){
var url="../chatroom.php?username="+encodeURI(obj.innerText)+"";
var name="";
var iWidth=500;
var iHeight=550;
var iTop = (window.screen.availHeight-30-iHeight)/2;
var iLeft = (window.screen.availWidth-10-iWidth)/2;
window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no');
}
function change(val,obj){
obj.style.cursor="pointer";
obj.style.backgroundColor="#FFFF00";
if(val=='user'){
list_user.style.display='block';
list_admin.style.display='none';
list_firend.style.display='none';
}else if(val=='admin'){
list_admin.style.display='block';
list_user.style.display='none';
list_firend.style.display='none';
}else if(val=='friend'){
list_firend.style.display='block';
list_user.style.display='none';
list_admin.style.display='none';
}
}
function moveOut(obj){
obj.style.cursor="default";
obj.style.backgroundColor="#CCCCCC";
}
</script>
<title>好友列表</title>
</head>
<body>
<div class="main">
<!--头部-->
<div class="top">
</div>
<!--用户列表-->
<div class="list">
<div class="list_menu">
<ul>
<li onMouseOver="change('admin',this)" onMouseOut="moveOut(this)"><img src="img/common/adminlist.png" /></li>
<li onMouseOver="change('user',this)" onMouseOut="moveOut(this)"><img src="img/common/userlist.png" /></li>
<li onMouseOver="change('friend',this)" onMouseOut="moveOut(this)"><img src="img/common/friendlist.png" /></li>
</ul>
</div>
<div id="list_admin" class="list_admin">
<ul>
<li>
<img src="img/user/face/jimmy.gif" />
<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)'>jimmy</a>
</li>
<li>
<img src="img/user/face/zarte.gif" />
<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)'>zarte</a>
</li>
<li>
<img src="img/common/none.gif" />
<a onClick='openChatRoom(this)' onMouseMove='moveOver(this)' onMouseOut='moveOut(this)'>暗流</a>
</li>
</ul>
<!--分页-->
<div class="fengye">
</div>
</div>
<div id="list_user" class="list_user">
<ul>
<?php getlist('u',$loginuser);?>
</ul>
<!--分页-->
<div class="fengye">
</div>
</div>
<div id="list_firend" class="list_firend">
<ul>
<?php getlist('f',$loginuser); ?>
</ul>
<!--分页-->
<div class="fengye">
</div>
</div>
</div>
<!--底部-->
<div class="bottom">
</div>
</div>
</body>
</html>