[实例: 数据库操作类(Model类-Model.class.php)]
整个类
Model.class.php
<?php
/*
1.作者: 123.
2.select: 查询表中所有数据.
3.find: 查询表中一条数据.
4.find($id): 查询表中某一条数据.
5.delete($id): 删除某一条数据,而且进行了安全防护,即不允许直接删除整表数据.
6.insert($post): 插入一条从表单传过来的数据.
7.update($post): 修改一条从表单传过来的数据.
*/
// Model.class.php
class Model{
// 表名属性
public $table;
public $connect;
//构造方法以及表名赋值
public function __construct($t){
$this->table=$t;
}
private function conn(){
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname = "myweb";
// 创建连接
if($conn=mysqli_connect($servername, $username, $password, $dbname)){
return $this->connect=$conn;
}
}
//select查询
public function select(){
$this->conn();
$sql="SELECT * FROM {$this->table}";
$rst=mysqli_query($this->connect,$sql);
while($row=mysqli_fetch_assoc($rst)){
$rows[]=$row;
}
return $rows;
}
// find
public function find($id){
if($id){
$where="where id=$id";
}
$this->conn();
$sql="SELECT * FROM {$this->table} {$where} limit 1";
$rst=mysqli_query($this->connect,$sql);
while($row=mysqli_fetch_assoc($rst)){
$rows[]=$row;
}
return $rows;
}
// delete
public function delete($id){
if($id){
$where="where id=$id";
$this->conn();
$sql="delete FROM {$this->table} {$where}";
if(mysqli_query($this->connect,$sql)){
return true;
}else{
return false;
}
}else{
return false;
}
}
// insert
public function insert($arr){
if($arr){
$this->conn();
foreach($arr as $key=>$val){
$keys[]=$key;
$vals[]='\''.$val.'\'';
}
$keystr=join(',',$keys);
$valstr=join(',',$vals);
// echo $keystr;
// echo '<br>';
// echo $valstr;
$sql="insert into {$this->table} ({$keystr}) values({$valstr}) ";
// echo $sql;
if(mysqli_query($this->connect,$sql)){
return true;
}else{
return false;
}
}else{
return false;
}
}
// update
public function update($arr){
if($arr){
$id=$arr['id'];
unset($arr['id']);
foreach($arr as $key=>$val){
$row[]="{$key}='{$val}'";
}
$str=join(',',$row);
$sql="update {$this->table} set {$str} where id={$id}";
echo $sql;
$this->conn();
if(mysqli_query($this->connect,$sql)){
return true;
}else{
return false;
}
}else{
return false;
}
}
}
?>
function.inc.php
<?php
function M($name){
return new Model($name);
}
?>
index.php
<?php
include "Model.class.php";
include 'function.inc.php';
$arr=array(
'id'=>10,
'username'=>'user20',
'age'=>'22',
'class_id'=>'2'
);
if(M('user')->update($arr)){
echo "<p>数据修改成功!</p>";
}else{
echo "<p>数据修改失败!</p>";
}
?>
1.查询所有数据
2.查询第一条数据
**
M('user')->find();
**
3.查询指定的一条数据
**
M('user')->find(5);
**
<?php
class Model{
// 表名属性
public $table;
public $connect;
//构造方法以及表名赋值
public function __construct($t){
$this->table=$t;
}
private function conn(){
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname = "myweb";
// 创建连接
if($conn=mysqli_connect($servername, $username, $password, $dbname)){
return $this->connect=$conn;
}
}
//select查询
public function select(){
$this->conn();
$sql="SELECT * FROM {$this->table}";
$rst=mysqli_query($this->connect,$sql);
while($row=mysqli_fetch_assoc($rst)){
$rows[]=$row;
}
return $rows;
}
// find
public function find($id){
if($id){
$where="where id=$id";
}
$this->conn();
$sql="SELECT * FROM {$this->table} {$where} limit 1";
$rst=mysqli_query($this->connect,$sql);
while($row=mysqli_fetch_assoc($rst)){
$rows[]=$row;
}
return $rows;
}
}
?>
**
<?php
function M($name){
return new Model($name);
}
?>
**
<?php
include "Model.class.php";
include 'function.inc.php';
$rows=M('user')->find(5);
echo '<pre>';
print_r($rows);
echo '</pre>';
?>
4.插入一条数据
**
<?php
include "Model.class.php";
include 'function.inc.php';
$arr=array(
'username'=>'user20',
'age'=>'22',
'class_id'=>'2'
);
if(M('user')->insert($arr)){
echo "<p>数据添加成功!</p>";
}else{
echo "<p>数据添加失败!</p>";
}
?>
**
public function insert($arr){
if($arr){
$this->conn();
foreach($arr as $key=>$val){
$keys[]=$key;
$vals[]='\''.$val.'\'';
}
$keystr=join(',',$keys);
$valstr=join(',',$vals);
// echo $keystr;
// echo '<br>';
// echo $valstr;
$sql="insert into {$this->table} ({$keystr}) values({$valstr}) ";
// echo $sql;
if(mysqli_query($this->connect,$sql)){
return true;
}else{
return false;
}
}else{
return false;
}
}
5.修改一条数据
**
<?php
include "Model.class.php";
include 'function.inc.php';
$arr=array(
'id'=>10,
'username'=>'user20',
'age'=>'22',
'class_id'=>'2'
);
if(M('user')->update($arr)){
echo "<p>数据修改成功!</p>";
}else{
echo "<p>数据修改失败!</p>";
}
?>
**
// update
public function update($arr){
if($arr){
$id=$arr['id'];
unset($arr['id']);
foreach($arr as $key=>$val){
$row[]="{$key}='{$val}'";
}
$str=join(',',$row);
$sql="update {$this->table} set {$str} where id={$id}";
echo $sql;
$this->conn();
if(mysqli_query($this->connect,$sql)){
return true;
}else{
return false;
}
}else{
return false;
}
}
6.删除一条数据
**
<?php
include "Model.class.php";
include 'function.inc.php';
if(M('user')->delete(1)){
echo "数据删除成功!";
}else{
echo "禁止删除整表数据!";
}
?>
**
public function delete($id){
if($id){
$where="where id=$id";
$this->conn();
$sql="delete FROM {$this->table} {$where}";
if(mysqli_query($this->connect,$sql)){
return true;
}else{
return false;
}
}else{
return false;
}
}
M函数原理
Model.class.php
<?php
class Model{
// 表名属性
public $table;
public $connect;
//构造方法以及表名赋值
public function __construct($t){
$this->table=$t;
}
private function conn(){
$servername = "localhost";
$username = "root";
$password = "133";
$dbname = "myweb";
// 创建连接
if($conn=mysqli_connect($servername, $username, $password, $dbname)){
return $this->connect=$conn;
}
}
//select查询
public function select(){
$this->conn();
$sql="SELECT * FROM {$this->table}";
$rst=mysqli_query($this->connect,$sql);
while($row=mysqli_fetch_assoc($rst)){
$rows[]=$row;
}
return $rows;
}
}
?>
function.inc.php
<?php
// 加工类对象
function M($name){
return new Model($name);
}
?>
index.php
<?php
include 'Model.class.php';
include 'function.inc.php';
M('user234')->say();
?>
**