日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php中model的用法是什么

本文操作環(huán)境:Windows7系統(tǒng)、php7.4版、DELL G3電腦

成都創(chuàng)新互聯(lián)公司專注于中小企業(yè)網(wǎng)站建設(shè)、策劃制作、運(yùn)行維護(hù),主要提供一站式的企業(yè)網(wǎng)站建設(shè)服務(wù)。建站類型:公司網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站制作獨(dú)立站等。創(chuàng)新互聯(lián)不是單一的建網(wǎng)站,而是結(jié)合企業(yè)的建站目標(biāo)去規(guī)劃網(wǎng)站怎么建,如何利于運(yùn)營,尋求適合的建站方案。其次,網(wǎng)站后臺(tái)操作的便捷性也是網(wǎng)站制作過程中的重點(diǎn),成都創(chuàng)新互聯(lián)公司的網(wǎng)站后臺(tái)簡單便捷,真正實(shí)現(xiàn)了零基礎(chǔ)操作。

php中model的用法是什么?

PHP模型Model類封裝數(shù)據(jù)庫操作示例

PHP模型Model類封裝數(shù)據(jù)庫操作,結(jié)合實(shí)例形式分析了php使用mysqli封裝的針對(duì)數(shù)據(jù)庫操作相關(guān)實(shí)現(xiàn)與使用技巧:

具體如下:

tableName = PRE.$tableName;
      //2.初始化連接數(shù)據(jù)庫
      $this->getConnect();
      //3.獲得當(dāng)前表的所有字段
      $this->getFields();
    }
    /**
     * 初始化連接數(shù)據(jù)庫操作
     */
    public function getConnect()
    {
      //1.連接數(shù)據(jù)庫
      $this->link = mysqli_connect(HOST,USER,PWD,DB,PORT);
      //2.判斷連接
      if (mysqli_connect_errno($this->link)>0){
        echo mysqli_connect_error($this->link);
        exit;
      }
    }
    /**
     * 執(zhí)行并發(fā)送SQL(查詢)
     * @param string $sql 要查詢的SQL語句
     * @return array 返回查詢出來的二維數(shù)組
     */
    public function query($sql)
    {
      $result = mysqli_query($this->link,$sql);
      if ($result && mysqli_num_rows($result)>0) {
       $arr = [];
       while($row = mysqli_fetch_assoc($result)){
          $arr[] = $row;
       }
      }
    return $arr;
    }
    /**
     * 獲取當(dāng)前表的所有字段
     */
    public function getFields()
    {
      //查看表結(jié)構(gòu)
      $sql = "desc {$this->tableName}";
      //執(zhí)行并發(fā)送SQL
      $result = $this->query($sql);
      $fields = [];
      foreach ($result as $k => $v){
        $fields[] = $v['Field'];
      }
      $this->allFields = $fields;
    }
    /**
     * 執(zhí)行并發(fā)送SQL語句(增刪改)
     * @param string $sql 要執(zhí)行的SQL語句
     * @return bool|int|string 添加成功則返回上一次操作id,刪除修改操作則返回true,失敗則返回false
     */
    public function exec($sql)
    {
      $result = mysqli_query($this->link,$sql);
      //處理結(jié)果集
      if ($result && mysqli_affected_rows($this->link)>0){
        //判斷是否為添加操作,是則返回上一次執(zhí)行的id
        if (mysqli_insert_id($this->link)){
          return mysqli_insert_id($this->link);
        }
        //刪除修改操作成功則返回true
        return true;
      }else{
        //未執(zhí)行成功則返回false
        return false;
      }
    }
    /**
     * 查詢多條數(shù)據(jù)
     */
    public function select()
    {
      $sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}";
      //執(zhí)行并發(fā)送SQL
      return $this->query($sql);
    }
    /**
     * 查詢一條數(shù)據(jù)
     * @param string $id 要查詢的id
     * @return array 返回一條數(shù)據(jù)
     */
    public function find($id="")
    {
      //判斷id是否存在
      if (empty($id)){
        $where = $this->where;
      }else{
        $where = "where id={$id}";
      }
      $sql = "select {$this->field} from {$this->tableName} {$where} limit 1";
      //執(zhí)行并發(fā)送sql
      $result = $this->query($sql);
      //返回一條數(shù)據(jù)
      return $result[0];
    }
    /**
     * 設(shè)置要查詢的字段信息
     * @param string $field 要查詢的字段
     * @return object 返回自己,保證連貫操作
     */
    public function field($field)
    {
      //判斷字段是否存在
      if (empty($field)){
        return $this;
      }
      $this->field = $field;
      return $this;
    }
    /**
     * 統(tǒng)計(jì)總條數(shù)
     * @return int 返回總數(shù)
     */
    public function count()
    {
      //準(zhǔn)備SQL語句
      $sql = "select count(*) as total from {$this->tableName} limit 1";
      $result = $this->query($sql);
      //返回總數(shù)
      return $result[0]['total'];
    }
    /**
     * 添加操作
     * @param array $data 要添加的數(shù)組
     * @return bool|int|string 添加成功則返回上一次操作的id,失敗則返回false
     */
    public function add($data){
      //判斷是否是數(shù)組
      if (!is_array($data)){
        return $this;
      }
      //判斷是否全是非法字段
      if (empty($data)){
        die("非法數(shù)據(jù)");
      }
      //過濾非法字段
      foreach ($data as $k => $v){
        if (!in_array($k,$this->allFields)){
          unset($data[$k]);
        }
      }
      //將數(shù)組中的鍵取出
      $keys = array_keys($data);
      //將數(shù)組中取出的鍵轉(zhuǎn)為字符串拼接
      $key = implode(",",$keys);
      //將數(shù)組中的值轉(zhuǎn)化為字符串拼接
      $value = implode("','",$data);
      //準(zhǔn)備SQL語句
      $sql = "insert into {$this->tableName} ({$key}) values('{$value}')";
      //執(zhí)行并發(fā)送SQL
      return $this->exec($sql);
    }
    /**
     * 刪除操作
     * @param string $id 要?jiǎng)h除的id
     * @return bool 刪除成功則返回true,失敗則返回false
     */
    public function delete($id="")
    {
      //判斷id是否存在
      if (empty($id)){
        $where = $this->where;
      }else{
        $where = "where id={$id}";
      }
      $sql = "delete from {$this->tableName} {$where}";
      echo $sql;
      //執(zhí)行并發(fā)送
      return $this->exec($sql);
    }
    /**
     * 修改操作
     * @param array $data 要修改的數(shù)組
     * @return bool 修改成功返回true,失敗返回false
     */
    public function update($data){
      //判斷是否是數(shù)組
      if (!is_array($data)){
        return $this;
      }
      //判斷是否是全是非法字段
      if(empty($data)){
        die("非法數(shù)據(jù)");
      }
      $str = "";
      //過濾非法字段
      foreach ($data as $k => $v){
        if ($k == "id"){
          $where = "where id={$v}";
          unset($data[$k]);
        }
        if (in_array($k,$this->allFields)){
          $str .= "{$k}='{$v}',";
        }else{
          unset($data[$k]);
        }
      }
      //判斷是否有條件
      if (empty($this->where)){
        die("請(qǐng)輸入條件");
      }
      //去掉最右側(cè)的逗號(hào)
      $str = rtrim($str,",");
      $sql = "update {$this->tableName} set {$str} {$this->where}";
      return $this->exec($sql);
    }
    /**
     * where條件
     * @param string $where 輸入的where條件
     * @return $this 返回自己,保證連貫操作
     */
    public function where($where)
    {
      $this->where = "where ".$where;
      return $this;
    }
    /**
     * order排序條件
     * @param string $order 以此為基準(zhǔn)進(jìn)行排序
     * @return $this 返回自己,保證連貫操作
     */
    public function order($order)
    {
      $this->order = "order by ".$order;
      return $this;
    }
    /**
     * limit條件
     * @param string $limit 輸入的limit條件
     * @return $this 返回自己,保證連貫操作
     */
    public function limit($limit)
    {
      $this->limit = "limit ".$limit;
      return $this;
    }
    /**
     * 析構(gòu)方法
     * 關(guān)閉數(shù)據(jù)庫連接
     */
    public function __destruct()
    {
      mysqli_close($this->link);
    }
  }
 //自行調(diào)試
 $a = new Model("表名");
 // var_dump($a->find(3));
 // var_dump($a->select());
  // var_dump($a->count());
  // $res = $a->select();
  //var_dump($res);
?>

分享題目:php中model的用法是什么
文章來源:http://www.dlmjj.cn/article/cogjjpd.html