网站制作学习网PHP→正文:php 常用数据库类
字体:

php 常用数据库类

PHP 2009/6/9 12:10:17  点击:不统计

关键词:php ,php类,mysql连接,数据库连接,php数据库连接类,php数据库操作

<?php

class Datelink

{

var $DateServer;//mysql数据库地址

var $DateBase;//mysql中的数据库

var $DateUser;//mysql数据库连接帐号

var $Datepwd;//mysql数据库连接密码

var $dbLink;//连接参数

function dbhalt($errmsg)

{//发生错误时调用的函数

$msg="database is wrong!";

$msg=$errmsg;

echo"$msg";

die();

}

function free_result($result){//释放资源

@mysql_free_result($result);

}

function DateContect()//数据库连接函数

{

$this->dbLink=@mysql_pconnect($this->DateServer,$this->DateUser,$this->Datepwd);//数据库连接mysql_pconnect常连接,mysql_connect短连接

if(!$this->dbLink) $this->dbhalt("exsiting error when connecting!");//如果发生错误,显示错误调用dbhalt函数

$this->dbbase=$this->DateBase;

if(!@mysql_select_db($this->dbbase,$this->dbLink))//@表示忽略 mysql 执行时的错误语句 "!"否定

$this->dbhalt("can't use this database,please check database!");//dbhalt调用错误函数

}



function get_rows($sql)//*直接返回记录集行数即有多少个记录集

{

return mysql_num_rows(mysql_query($sql));

}

function fetch_array($result)//定义fetch_array取得execute返回的数组

{

$result = $this->result;

return mysql_fetch_array($result);

}



function execute($sql){//*执行SQL语句返回记录集

$this->result=mysql_query($sql);

return $this->result;

}

function insetr($sql)//插入数据

{

$result=$this->execute($sql,$DateBase);

$this->insert_id=mysql_insert_id($this->dbLink);

$this->free_result($result);

return $this->insert_id;

}

function update($sql)//更新数据

{

$result=$this->execute($sql,$DateBase);

$this->affected_rows=mysql_affected_rows($this->dbLink);

$this->free_result($result);

return $this->affected_rows;

}

//

function delete($sql){//删除数据

$result=$this->execute($sql,$DateBase);

$this->affected_rows=mysql_affected_rows($this->dbLink);

$this->free_result($result);

return $this->affected_rows;

}

function dbclose(){//关闭数据库

mysql_close($this->dbLink);

}

//*返回行数************************

function num_rows($result)

{

return mysql_num_rows($result);

}

}//结束class的括号



$db=new Datelink();

$db->DateServer= 'addr';

$db->DateBase= 'date_name';

$db->DateUser= 'root';

$db->Datepwd = '';

$db->DateContect();

?>

·上一篇:输出php数组值 >>    ·下一篇:修改服务器远程登录端口 >>
推荐文章
最新文章