接口类
<?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 "删除失败";
}
?>