封装接口类

接口类

<?php
	header("content-type:text/html;charset=utf8");
	//interface声明接口
	interface  Db{
	   public function find($table,$where);
	   public function select($table);
	   public function del($table,$where);
	}
	//调用接口的关键字
	class People implements Db{
		public $host;
		public $username;
		public $pwd;
		public $db_name;
		public $charset;
	public 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);
	}
     //查询单条数据
	public function find($table,$where){
         $sql="select * from $table where $where";
		 $res=mysql_query($sql);
		 $arr=mysql_fetch_assoc($res);
		 return $res;
	 }
	 //查询所有数据
     public function select($table){
         $sql="select * from $table";
		 $res=mysql_query($sql);
		 return $res;
	 }
	 //删除数据
	public function del($table,$where){
         $sql="delete from $table where $where";
		 $res=mysql_query($sql);
		 return $res;
	 }
	}
?>
视图层展示
<?php
require("db.class.php");
$people = new People('127.0.0.1','root','root','php3yue','set names utf8');
$arr=$people->select('news');
?>
<table border=1>
<tr>
	<td>ID</td>
	<td>标题</td>
	<td>内容</td>
	<td>操作</td>
</tr>
<?php while($arr1=mysql_fetch_assoc($arr)){ ?>
<tr>
	<td><?php echo $arr1['id']?></td>
	<td><?php echo $arr1['title']?></td>
	<td><?php echo $arr1['content']?></td>
	<td><a href="find.php?id=<?php echo $arr1['id']?>">删除</a></td>
</tr>
<?php } ?>
</table>

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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值