栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php DB类 数据库操作类

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php DB类 数据库操作类


class DB

{

static $link=null;  //mysql服务器连接的资源

private $host="";   //mysql服务器主机名

private $user="";   //mysql的用户名

private $password="";  //mysql的密码

private $db="";     //访问的数据库名



//构造方法

function __construct($db='',$host="127.0.0.1",$user="root",$password=''){

 $this->host=$host;

 $this->user=$user;

 $this->password=$password;

 $this->db=$db;

      if(self::$link==null)

 {

self::$link=mysql_connect($this->host, $this->user,$this->password);

mysql_select_db($this->db,self::$link);

   $this->query('SET NAMES utf8');

 }  

}



function selectDb($db){

return mysql_select_db($db) or die('选择数据库失败'.mysql_error());

}



function getRow($sqlStr,$result_type=MYSQL_ASSOC)

{

$rs=@mysql_query($sqlStr,self::$link) or $this->showError("查询语句有错误!$sqlStr");

if($rs)

{

$row=@mysql_fetch_array($rs,$result_type);

return $row;

}


return false;

}



function getAll($sql,$result_type=MYSQL_ASSOC)

{

$rs=@mysql_query($sql,self::$link) or $this->showError("查询语句有错误!$sql");

if($rs===false)

{

return false;

}

else if($rs===true){

return true;

}

else{

$arr=array();

while($row=@mysql_fetch_array($rs,$result_type))

{

$arr[]=$row;

}

return $arr;

}

}



function getObjects($sql){

 $rs=mysql_query($sql,self::$link) or $this->showError("查询语句有错误!");

 if($rs===false){

 return false;

 }

 $arr=array();

 while($row=@mysql_fetch_object($rs)){

$arr[]=$row;

 }

 return $arr;

}



   

function query($sql)

{

$bl = mysql_query($sql,self::$link) or $this->showError();

return $bl;

}



function showError($error="")

{

echo $error;

echo '
';

echo mysql_error();

echo '
';

}

 


function insert($table,$dataArray){

$fields="";

$values="";

   if(!is_array($dataArray) || count($dataArray)<=0) {

            $this->showError('没有要插入的数据');

            return false;

        }


$fields=implode(',',array_keys($dataArray));

$values=implode("','",array_values($dataArray));

$values="'".$values."'";

$sql = "insert into $table($fields) values($values)";

   return mysql_query($sql) or $this->showError("插入语句有错误!$sql");

}



   

    public function update($table,$dataArray,$condition="1=1") {

        if( !is_array($dataArray) || count($dataArray)<=0) {

            $this->showError('没有要更新的数据');

            return false;

        }

        $value = "";

$field = "";

        foreach($dataArray as $key=>$val) {

            $value .="$key = '$val',";

        }

        $value = substr( $value,0,-1);

        $sql = "update $table set $value where $condition";


return mysql_query($sql) or $this->showError("更新语句有错误!$sql");

    }



   

    public function delete($table,$condition="") {

        if(empty($condition)) {

$sql="delete from $table";

        }

else if(is_string($condition)){

$sql="delete from $table where $condition";

}

return mysql_query($sql) or $this->showError("删除语句有错误!$sql");

    }



function closeDb()

{

mysql_close(self::$link);

}

}



?>


使用:

//添加

 include("./db.class.php");

 $db=new DB("db");

 $arr=array("name"=>$user,"pwd"=>$pwd);

 $add=$db->insert("biao",$arr);


//取所有数据

 include("../db.class.php");

 $db=new DB("db");

 $arr=$db->getAll("select * from message");


//取单条数据

 include("../db.class.php");

 $db=new DB("db");

 $arr=$db->getRow("select * from message");


//删除数据

 include '../db.class.php';

 $db = new DB('db');

 $str ="id=$id";

 $r = $db->delete('biao',$str);

$db->delete("ajax_update","id=$id");



//修改

$id=$_GET['id'];

$arr['title']=$title;

$db->update('ajax_update',$arr,"id=$id");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/227120.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号