封装数据库类curd

<?php
header("content-type:text/html;charset=utf8");
  class DB{
	 public $host;
	 public $username;
	 public $pwd;
	 public $db_name;
	 public $charset;
	  //构造方法
      function __construct($host,$username,$pwd,$db_name,$charset){
          $this->host=$host;
		  $this->username=$username;
		  $this->pwd=$pwd;
          $this->db_name=$db_name;
          $this->charset=$charset;
           mysql_connect($this->host,$this->username,$this->pwd);
           mysql_select_db($this->db_name);
		   mysql_query($this->charset);
}
     //添加
	  function add($table,$first,$second){
          $sql="insert into ".$table."(".$first.") values(".$second.")";
		  $res=mysql_query($sql);
		  return $res;
	  }
     //查询所有数据
	 function select($table){
        $sql="select * from $table";
        $res=mysql_query($sql);
		$list = array();
        while($arr=mysql_fetch_assoc($res)){
         $list[]=$arr;
    }
	 return $list;
	 }
	 //查询单条数据
	 function find($table,$where){
        $sql="select * from $table where $where";
        $res=mysql_query($sql);
		$arr=mysql_fetch_assoc($res);
		return $arr;
	 }
       //两表联查
    public function TwoTable($table1,$table2,$id1,$id2,$id3,$limit){

        $sql="select * from $table1 inner join $table2 on $table1.$id1=$table2.$id2 order by $id3 $limit";
        $res= mysql_query($sql);
        $ar=array();
        while($arr=mysql_fetch_assoc($res)){
            $ar[]=$arr;
        }
        return $ar;
    }
    //模糊查询
    public function Limit($table,$offset,$length){
        $sql="select * from $table limit $offset,$length";
        //echo $res;
        $res= mysql_query($sql);
        $arr=array();
        while($aar=mysql_fetch_assoc($re)){
            $arr[]=$aar;
        }
        return $arr;
  }
      //删除
	  function del($table,$where){
         $sql="delete  from $table where $where";
        $res=mysql_query($sql);
		return $res;
	 }
		  
	// 修改
	 function save($table,$first,$where){
         $sql="update $table set $first  where $where";
        $res=mysql_query($sql);
		return $res;
  }
  }
?>


增加

<?php
require("db.class.php");
$db = new DB('127.0.0.1','root','root','php3yue','set names utf8');
$first="title,content";
$second ="'刘伟超','更多是一个'";
$res=$db->add('news',$first,$second);
if($res){
   echo "添加成功";
}else{
   echo "添加失败";
}
?>

删除
<?php
require("db.class.php");
$db = new DB('127.0.0.1','root','root','php3yue','set names utf8');
$res=$db->del('news','id=2');
if($res){
   echo "删除成功";
}else{
   echo "删除失败";
}
?>

 修改
<?php
require("db.class.php");
$db = new DB('127.0.0.1','root','root','php3yue','set names utf8');
$res=$db->save('news',"title='刘宁'",'id=1');
if($res){
   echo "修改成功";
}else{
   echo "修改失败";
}
?>

查询
<?php
require("db.class.php");
$db = new DB('127.0.0.1','root','root','php3yue','set names utf8');
$arr= $db->select('news');
 foreach($arr as $key=>$value){
    echo $value['title'];
	echo "</br>";
 }

?>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值