<?php
class Model{
public $link;
public $tabName;
//链接数据坤
//初始化,构造方法
public function __construct($tabName){
$this->link = mysqli_connect('localhost','root','123456');
if(mysqli_connect_errno($this->link)>0){
echo mysqli_connect_error($this->link);exit;
}
mysqli_select_db($this->link,'day2');
mysqli_set_charset($this->link,'utf8');
//初始化数据表
$this->tabName = $tabName;
}
//增
public function add(){
$sql ="INSERT INTO{$this -> tabName}
VALUES('写啥?','写啥?','写啥?','写啥?','写啥?')";
$result = mysqli_query($this->link,$sql);
if($result && mysqli_affected_rows($this->link)>0){
return true;
}else{
return false;
}
}
//删
public function Del(){
$sql = "DELETE FROM {$this -> tabName} WHERE id='2'";
$result = mysqli_query($result,$sql);
if($result && mysqli_affected_rows($result)){
return true;
}else{
return false;
}
}
public function Update(){
$sql = "UPDATE {$this -> tabName}
SET name='',age='',city='',sex='' WHERE id='22'" ;
$result = mysqli_query($this->link,$sql);
if($result&& mysqli_affected_rows($result)){
return true;
}else{
return false;
}
}
//查
public function Select(){
$sql = "SELECT * FROM {$this -> tabName}";
$result = mysqli_query($this->link,$sql);
if($result && mysqli_num_rows($result)>0){
$userlist = array();
while($row = mysqli_fetch_assoc($result)){
$userlist[] = $row;
}
}
return $userlist;
}
//析构
public function __destruct(){
mysqli_close($this->link);
}
}
$a = new Model('info');
//查询办法
$userlist = $a->Select();
var_dump($userlist);点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号