ITPub博客

首页 > IT职业 > IT职场 > php DB类 数据库操作类

php DB类 数据库操作类

IT职场 作者:myiee 时间:2021-09-09 09:19:21 0 删除 编辑


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)

            $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)

            $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");


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3702/viewspace-2809462/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    89
  • 访问量
    863