数据库类php,数据库操作类(PHP/mysql)

var$dbhostname;

var$dbname;

var$dbuser;

var$dbpassword;

var$connid;

var$query;

var$result;

var$resultid;

var$insertid;

var$rows;

var$errCode=0;

var$errString="";

var$debugString="";

var$xml="";/* 构造函数 */functionDatabase()

{$this->dbhostname="localhost";//登录数据库主机名$this->dbname="yourdb";/数据库名$this->dbuser="root";//数据库登录用户名$this->dbpassword="";//数据库登录密码$this->_connect();//连接数据库$this->_db_select();//选择数据库}/* 数据库执行函数, $type 执行操作类型, ""为查询, "update"为更新, "insert"为插入, "delete"为删除 */functiondb_query($sql,$type)

{$this->query=$sql;

if ($this->_query())

{

if(empty($type))

{$this->_num_rows();//返回查询的函数$this->result=$this->_fetch_array();//返回查询结果}

if ($type=="insert")

{$this->insertid=@mysql_insert_id();//返回插入的ID自动增量}

returntrue;

}

else

returnfalse;

}/* 数据库结果生成XML */functioncreateXML($itemroot="child",$item="root")

{$dom= newDOMDocument("1.0","utf-8");$dom->preserveWhiteSpace=false;$dom->formatOutput=true;$rootNode=$dom->createElement($item);

for($i=0;$iresult);$i++)

{$itemNode=$dom->createElement($itemroot);

foreach($this->result[$i] as$key=>$value)

{//$value = iconv("gb2312","utf-8",$value);$tmpNode=$dom->createElement($key,$value);$itemNode->appendChild($tmpNode);

}$rootNode->appendChild($itemNode);

}$dom->appendChild($rootNode);

if ($this->xml=$dom->saveXML())

return$this->xml;

else

returnfalse;

}/* 连接数据库 */function_connect()

{$this->connid= @mysql_connect($this->dbhostname,$this->dbuser,$this->dbpassword);

if (false==$this->connid)

{$this->errCode=1;$this->errString="不能连接数据库!";$this->debugString="不能连接数据库: ".mysql_error();$this->showErr();

returnfalse;

}

else

returntrue;

}/* 选择数据库 */function_db_select()

{$selectFlag= @mysql_select_db($this->dbname,$this->connid);

if (false==$selectFlag)

{$this->errCode=2;$this->errString="不能选择数据库!";$this->debugString="不能选择数据库: ".$this->dbname;$this->showErr();

returnfalse;

}

else

returntrue;

}/* 数据库查询语句 */function_query()

{$this->resultid= @mysql_query($this->query,$this->connid);

if (false==$this->resultid)

{$this->errCode=3;$this->errString="数据库查询语句错误!";$this->debugString="数据库查询语句错误: ".$this->query.mysql_error();$this->showErr();

returnfalse;

}

else

returntrue;

}/* 返回查询记录总数 */function_num_rows()

{$this->rows= @mysql_num_rows($this->resultid);

}/* 返回结果数组 */function_fetch_array()

{

if ($this->errCode!=3)

{$tmpResult="";$i=0;

while ($row=mysql_fetch_array($this->resultid,MYSQL_ASSOC))

{$tmpResult[$i] =$row;$i++;

}//end while.return empty($tmpResult) ?NULL:$tmpResult;

}

}/* 显示错误信息 $debugString用于调试,$errString用于给客户显示 */functionshowErr()

{//echo $this->debugString."/n";}/* 关闭连接,释放内存 */functionclose()

{

@mysql_free_result($this->resultid);

@mysql_close($this->connid);

}

function__destruct()

{

if($this->connid)

{$this->close();

}

}

}/* 数据库查询类  end*/?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值