久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類【定義與用法】

這篇文章主要介紹了PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類,結(jié)合實例形式分析了php封裝mysqli函數(shù)實現(xiàn)的數(shù)據(jù)庫操作類定義及連接、增刪改查數(shù)據(jù)庫等基本操作用法,需要的朋友可以參考

本文實例講述了PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫連接工具類。分享給大家供大家參考,具體如下:

mysql.class.php:

<?php
class mysql
{
  private $mysqli;
  private $result;
  /**
   * 數(shù)據(jù)庫連接
   * @param $config 配置數(shù)組
   */
  public function connect($config)
  {
    $host = $config['host'];    //主機地址
    $username = $config['username'];//用戶名
    $password = $config['password'];//密碼
    $database = $config['database'];//數(shù)據(jù)庫
    $port = $config['port'];    //端口號
    $this->mysqli = new mysqli($host, $username, $password, $database, $port);
  }
  /**
   * 數(shù)據(jù)查詢
   * @param $table 數(shù)據(jù)表
   * @param null $field 字段
   * @param null $where 條件
   * @return mixed 查詢結(jié)果數(shù)目
   */
  public function select($table, $field = null, $where = null)
  {
    $sql = "SELECT * FROM {$table}";
    if (!empty($field)) {
      $field = '`' . implode('`,`', $field) . '`';
      $sql = str_replace('*', $field, $sql);
    }
    if (!empty($where)) {
      $sql = $sql . ' WHERE ' . $where;
    }
    $this->result = $this->mysqli->query($sql);
    return $this->result->num_rows;
  }
  /**
   * @return mixed 獲取全部結(jié)果
   */
  public function fetchAll()
  {
    return $this->result->fetch_all(MYSQLI_ASSOC);
  }
  /**
   * 插入數(shù)據(jù)
   * @param $table 數(shù)據(jù)表
   * @param $data 數(shù)據(jù)數(shù)組
   * @return mixed 插入ID
   */
  public function insert($table, $data)
  {
    foreach ($data as $key => $value) {
      $data[$key] = $this->mysqli->real_escape_string($value);
    }
    $keys = '`' . implode('`,`', array_keys($data)) . '`';
    $values = '\'' . implode("','", array_values($data)) . '\'';
    $sql = "INSERT INTO {$table}( {$keys} )VALUES( {$values} )";
    $this->mysqli->query($sql);
    return $this->mysqli->insert_id;
  }
  /**
   * 更新數(shù)據(jù)
   * @param $table 數(shù)據(jù)表
   * @param $data 數(shù)據(jù)數(shù)組
   * @param $where 過濾條件
   * @return mixed 受影響記錄
   */
  public function update($table, $data, $where)
  {
    foreach ($data as $key => $value) {
      $data[$key] = $this->mysqli->real_escape_string($value);
    }
    $sets = array();
    foreach ($data as $key => $value) {
      $kstr = '`' . $key . '`';
      $vstr = '\'' . $value . '\'';
      array_push($sets, $kstr . '=' . $vstr);
    }
    $kav = implode(',', $sets);
    $sql = "UPDATE {$table} SET {$kav} WHERE {$where}";
    $this->mysqli->query($sql);
    return $this->mysqli->affected_rows;
  }
  /**
   * 刪除數(shù)據(jù)
   * @param $table 數(shù)據(jù)表
   * @param $where 過濾條件
   * @return mixed 受影響記錄
   */
  public function delete($table, $where)
  {
    $sql = "DELETE FROM {$table} WHERE {$where}";
    $this->mysqli->query($sql);
    return $this->mysqli->affected_rows;
  }
}

使用方法

<?php
require_once 'mysql.class.php';
/* 配置連接參數(shù) */
$config = array(
  'type' => 'mysql',
  'host' => 'localhost',
  'username' => 'woider',
  'password' => '3243',
  'database' => 'php',
  'port' => '3306'
);
/* 連接數(shù)據(jù)庫 */
$mysql = new mysql();
$mysql->connect($config);
/* 查詢數(shù)據(jù) */
//1、查詢所有數(shù)據(jù)
$table = 'mysqli';//數(shù)據(jù)表
$num = $mysql->select($table);
echo '共查詢到' . $num . '條數(shù)據(jù)';
print_r($mysql->fetchAll());
//2、查詢部分數(shù)據(jù)
$field = array('username', 'password'); //過濾字段
$where = 'id % 2 =0';          //過濾條件
$mysql->select($table, $field, $where);
print_r($mysql->fetchAll());
/* 插入數(shù)據(jù) */
$table = 'mysqli';//數(shù)據(jù)表
$data = array(  //數(shù)據(jù)數(shù)組
  'username' => 'admin',
  'password' => sha1('admin')
);
$id = $mysql->insert($table, $data);
echo '插入記錄的ID為' . $id;
/* 修改數(shù)據(jù) */
$table = 'mysqli';//數(shù)據(jù)表
$data = array(
  'password' => sha1('nimda')
);
$where = 'id = 44';
$rows = $mysql->update($table, $data, $where);
echo '受影響的記錄數(shù)量為' . $rows . '條';
/* 刪除數(shù)據(jù) */
$table = 'mysqli';
$where = 'id = 45';
$rows = $mysql->delete($table, $where);
echo '已刪除' . $rows . '條數(shù)據(jù)';

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計有所幫助。

【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學習交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。

相關(guān)文檔推薦

這篇文章主要介紹了PHP有序表查找之插值查找算法,簡單分析了插值查找算法的概念、原理并結(jié)合實例形式分析了php實現(xiàn)針對有序表插值查找的相關(guān)操作技巧,需要的朋友可以參考下
下面小編就為大家分享一篇ThinkPHP整合datatables實現(xiàn)服務(wù)端分頁的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP實現(xiàn)APP微信支付的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP實現(xiàn)的多維數(shù)組排序算法,結(jié)合實例形式對比分析了php針對多維數(shù)組及帶有鍵名的多維數(shù)組進行排序相關(guān)操作技巧與注意事項,需要的朋友可以參考下
這篇文章主要為大家詳細介紹了php結(jié)合ajaxuploadfile實現(xiàn)無刷新文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本篇文章給大家詳細介紹了PHP開發(fā)接口使用RSA進行加密解密方法,對此有興趣的朋友可以學習下。
主站蜘蛛池模板: 国产精品一区在线观看 | 亚洲国产一区视频 | 精品视频在线观看 | 欧美成人精品一区二区三区 | 中文字幕在线视频精品 | 亚洲成人一二三 | 欧美午夜影院 | 欧美激情一区二区三区 | 一区二区三区高清 | 日韩亚洲欧美综合 | 国产色黄 | 北条麻妃99精品青青久久主播 | 国产亚洲日本精品 | 精品一区在线 | 欧美在线日韩 | 琪琪午夜伦伦电影福利片 | 久久久久国产一区二区三区四区 | 狠狠久久久 | 久草.com | 日韩中文在线视频 | 午夜综合| 日韩视频在线播放 | 久久综合一区 | 给我免费的视频在线观看 | 日本成人免费网站 | 亚洲国产精品一区二区久久 | 欧美日韩在线精品 | 欧美性成人 | 日韩国产一区二区 | 亚洲乱码一区二区三区在线观看 | 欧美日韩综合精品 | 精品一区电影 | 国产精品亚洲一区 | 岛国av免费看 | 特级做a爰片毛片免费看108 | 99精品视频一区二区三区 | 国产精品久久久久久久久久 | 久热免费| 97精品超碰一区二区三区 | 国产片侵犯亲女视频播放 | 久久中文字幕一区 |