Blogger Information
Blog 250
fans 3
comment 0
visits 321582
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql 写入到类当中的案例
梁凯达的博客
Original
820 people have browsed it

实例

<?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);

运行实例 »

点击 "运行实例" 按钮查看在线实例

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post