标签:public
-----------------------------------config.php---------------------------------------------
<?php
//主机名
define(‘HOST‘,‘localhost‘);
//用户名
define(‘USER‘, ‘root‘);
// 密码
define(‘PWD‘, ‘123456‘);
//数据库
define(‘DB‘,‘ss21‘);
//字符集
define(‘CHARSET‘,‘utf8‘);
--------------------------------index.php--------------------------------------------------
<?php
include ‘./config.php‘;
//echo HOST;exit;
include ‘./Model.class.php‘;
//实例化对象
$user = new Model(‘info‘);
//查询内容
$userlist=$user->select();
//var_dump($userlist);
// //调用查询一条内容的方法
// $userinfo = $user->find(4);
// var_dump($userinfo);
// $bool=$user->del(3);
// if($bool){
// echo ‘删除成功‘;
// }else{
// echo ‘删除失败‘;
// }
-------------------------------Model.class.php---------------------------------------------
<?php
//定义一个操作数据库类
class Model{
public $tabName;//存储表名的
public $link;//连接数据的对象
//构造方法里面连接数据库
public function __construct($tabName){
//初始化数据库连接
$this->getConnect();
//将你要操作的数据表存储起来
$this->tabName = $tabName;
}
//查询
public function select(){
//5.
$sql="SELECT * FROM {$this->tabName}";
//echo $sql;exit;
return $this->query($sql);
}
//查询一条结果
public function find($id){
//你查询的一条结果sql语句
$sql="SELECT * FROM {$this->tabName} WHERE id={$id}";
//调用发送sql语句函数
return $this->query($sql);
//var_dump($list);
}
//统计总条数
public function count(){
//返回值是字符串
}
//增加
//$data[‘name‘]=‘zhansan‘;
//$data[‘age‘]=18;
//$data[‘sex‘]=1;
//$data[‘city‘]=‘沈阳‘;
public function add($data){
$sql="INSERT INTO {$this->tabName} VALUES(‘‘,‘‘,‘‘)";
return $this->execute($sql);
}
//删除思路 传递一个参数 这个参数是id值
public function del($id){
$sql="DELETE FROM {$this->tabName} WHERE id={$id}";
return $this->execute($sql);
}
//修改
public function update(){
$sql="UPDEATE {$this->tabName} SET WHERE id=";
return $this->execute($sql);
}
/****************辅助方法***************************/
//连接数据库的方法
protected function getConnect(){
$this->link=mysqli_connect(HOST,USER,PWD);
if(mysqli_connect_errno($this->link)){
echo mysqli_connect_error($this->link);exit;
}
mysqli_select_db($this->link,DB);
mysqli_set_charset($this->link,CHARSET);
}
//用于 添加 修改 删除
protected function execute($sql){
$result = mysqli_query($this->link,$sql);
if($result && mysqli_affected_rows($this->link)>0){
if(mysqli_insert_id($this->link)){
return mysqli_insert_id($this->link);
}
return true;
}else{
return false;
}
}
//查询操作
protected function query($sql){
$result = mysqli_query($this->link,$sql);
if($result && mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
$list[]=$row;
}
}
return $list;
}
//析构方法
public function __destruct(){
mysqli_close($this->link);
}
}
标签:public
原文地址:http://13346331.blog.51cto.com/13336331/1981798