本文实例讲述了PHP数据库表操作的封装类及用法。分享给大家供大家参考,具体如下:

数据库表结构:

CREATE TABLE `test_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`nickname` varchar(45) NOT NULL,
`r` tinyint(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `test_blog` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `user_id` int(11) NOT NULL,
 `title` varchar(45) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

设置字符编码:

header('Content-Type: text/html; charset=utf-8');

引入Table类:

require 'Table.php';

设置数据库参数:

Table::$__host = '127.0.0.1:3306';
Table::$__user = 'root';
Table::$__pass = '123456';
Table::$__name = 'test';
Table::$__charset = 'utf8';

创建实体对象:

Table类有三个参数: $table, $pk, $pdo=null

$table: 表名称.
$pk: 主键名称. 不支持联合主键
$pdo: 独立的PDO对象. 一般不需要传
Notice: Table类是表操作的封装,不是Model层的基类,所以不支持表前缀,表前缀应该在Model层实现

$userTable = new Table('test_blog');
$blogTable = new Table('test_blog');

插入数据:

$user = array(
    'username' => "admin1",
    'password' => "admin1",
    'nickname' => "管理员1",
    'r' => mt_rand(0, 5),
);
echo $userTable->insert($user)->rowCount(), "\n";
echo $userTable->lastInsertId(), "\n";

批量插入数据:

$fields = array('username','password','nickname','r');
for ($i=2; $i<=100; $i++) {
  $rows[] = array("admin$i", "admin$i", "管理员$i", mt_rand(0, 5));
}
$userTable->batchInsert($fields, $rows);

查询所有数据:

select方法返回一个PDOStatement对象, fetchAll返回多行, fetch返回单行

var_dump($userTable->select()->fetchAll());

field自定义:

var_dump($userTable->select('id,nickname')->fetchAll());

where查询:

var_dump($userTable->where('id > "htmlcode">
var_dump($userTable->where('id > "htmlcode">
var_dump($userTable->where('id = "htmlcode">
var_dump($userTable->group('r')->having('c between "htmlcode">
var_dump($userTable->order('r desc, id')->select()->fetchAll());

limit 行数:

跳过30行 返回10行

var_dump($userTable->limitOffset(10, 30)->select()->fetchAll());

查询单行:

var_dump($userTable->where('id = "htmlcode">
var_dump($userTable->find(4));

update更新数据:

$user = array( 'username' => 'admin4-1', 'nickname' => '管理员4-1', );
echo $userTable->where('id = "\n";

replace替换数据:

使用了MySQL的REPLACE语句

$user = array(
    'id' => 4,
    'username' => 'admin4',
    'password' => 'admin4',
    'nickname' => '管理员4',
    'r' => mt_rand(0, 5),
);
echo $userTable->replace($user)->rowCount(), "\n";

删除数据:

echo $userTable->where('id = "\n";

分页查询

第2页, 每页10行数据:

var_dump($userTable->page(2, 10)->select()->fetchAll());

分页查询的总行数:

$userTable->where('r="\n";

复杂查询:

var_dump($userTable->where('id > "htmlcode">
$id = 2;
// 加一
var_dump($userTable->where('id = "htmlcode">
$id = 2;
// 加一
echo $userTable->where('id = "\n";
// 减一
echo $userTable->where('id = "\n";

save 保存修改:

判断数据中是否存在主键字段,如果存在主键字段就update数据,反之insert数据

// 修改
$user = array(
  'id' => 3,
  'nickname' => '管理员3-3',
);
echo $userTable->save($user)->rowCount(), "\n";
var_dump($userTable->find(3));
// 添加
$user = array(
    'username' => 'admin11',
    'password' => 'admin11',
    'nickname' => '管理员11',
    'r' => mt_rand(0, 5),
);
echo $userTable->save($user)->rowCount(), "\n";
$id = $userTable->lastInsertId();
var_dump($userTable->find($id));

生成外表测试数据:

$users = $userTable->select('id')->fetchAll();
$id = 0;
foreach ($users as $user) {
  for ($i=0; $i<10; $i++) {
    $id++;
    $blog = array(
        'user_id' => $user['id'],
        'title' => "blog$id",
    );
    $blogTable->insert($blog);
  }
}

Table类不支持JOIN查询

需要的朋友可以手写sql语句,使用query方法来执行.或者自己修改Table类来支持JOIN

获取外表数据:

$blogs = $blogTable->where('id in ("htmlcode">
// 获取映射数据
var_dump($userTable->select('*, id')->fetchAll(PDO::FETCH_UNIQUE));
// 获取数组
var_dump($userTable->select('nickname')->fetchAll(PDO::FETCH_COLUMN));
// 获取键值对
var_dump($userTable->select('id, nickname')->fetchAll(PDO::FETCH_KEY_PAIR));
// 获取数据分组
var_dump($userTable->select('r, id, nickname')->fetchAll(PDO::FETCH_GROUP));
// 获取数据分组
var_dump($userTable->select('r, id')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN));
// 获取数据分组
var_dump($userTable->select('r, nickname')->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_KEY_PAIR));
// 获取对象 指定获取方式,将结果集中的每一行作为一个属性名对应列名的对象返回。
var_dump($userTable->select()->fetchAll(PDO::FETCH_OBJ));
// 获取对象 指定获取方式,返回一个所请求类的新实例,映射列到类中对应的属性名。
// Note: 如果所请求的类中不存在该属性,则调用 __set() 魔术方法
var_dump($userTable->select()->fetchAll(PDO::FETCH_CLASS));
// 获取对象 指定获取方式,更新一个请求类的现有实例,映射列到类中对应的属性名。
var_dump($userTable->select()->fetchAll(PDO::FETCH_INTO));
// 获取自定义行
var_dump($userTable->select()->fetchAll(PDO::FETCH_FUNC, function($id, $username, $password, $r){
  return array('id'=>$id, 'name'=>"$username - $password - $r");
}));
// 获取单一值
var_dump($userTable->select()->fetchAll(PDO::FETCH_FUNC, function($id, $username, $password, $r){
  return "$id - $username - $password - $r";
}));

Table类源代码:

<"mysql:host=%s;dbname=%s;charset=%s;", self::$__host, self::$__name, self::$__charset);
    $options = array(
        PDO::ATTR_PERSISTENT => true,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    );
    return self::$__pdo = new PDO($dsn, self::$__user, self::$__pass, $options);
  }
  /**
   * 执行语句
   * @param string $sql
   * @return PDOStatement
   */
  public function query($sql) {
    $params = func_get_args();
    array_shift($params);
    return $this->queryParams($sql, $params);
  }
  /**
   * 执行语句
   * @param string $sql
   * @return PDOStatement
   */
  public function queryParams($sql, array $params) {
    $sqls = explode('"\n"; var_dump($params_new); // exit();
    $stmt->executeResult = $stmt->execute();
    $this->reset();
    return $stmt;
  }
  /**
   * 查询数据
   * @param string $field
   * @return PDOStatement
   */
  public function select($columns='*') {
    $params = array_merge($this->_where_params, $this->_having_params);
    $sql = "SELECT $columns FROM `{$this->_table}`";
    $sql .= empty($this->_where) "INSERT `{$this->_table}` SET";
    $params = array();
    foreach ($data as $col=>$val) {
      $sql .= " `$col` = ";
      $params[] = $val;
    }
    $sql{strlen($sql)-1} = ' ';
    return $this->queryParams($sql, $params);
  }
  /**
   * 批量插入数据
   * @param array $names
   * @param array $rows
   * @param number $batch
   * @return Table
   */
  public function batchInsert(array $fields, array $rows, $batch=1000) {
    $i = 0;
    $sql = "INSERT `{$this->_table}` (`".implode('`, `', $fields)."`) VALUES ";
    foreach ($rows as $row) {
      $i++;
      $sql .= "('".implode("','", array_map('addslashes', $row))."'),";
      if ($i >= $batch) {
        $sql{strlen($sql)-1} = ' ';
        $this->query($sql);
        $i = 0;
        $sql = "INSERT `{$this->_table}` (`".implode('`, `', $fields)."`) VALUES ";
      }
    }
    if ($i > 0) {
      $sql{strlen($sql)-1} = ' ';
      $this->query($sql);
    }
    return $this;
  }
  /**
   * 更新数据
   * @param array $data
   * @return PDOStatement
   */
  public function update(array $data) {
    $sql = "UPDATE `{$this->_table}` SET";
    $params = array();
    foreach ($data as $col=>$val) {
      $sql .= " `$col` = ";
      $params[] = $val;
    }
    $sql{strlen($sql)-1} = ' ';
    $sql .= empty($this->_where) "REPLACE `{$this->_table}` SET";
    $params = array();
    foreach ($data as $col=>$val) {
      $sql .= " `$col` = ";
      $params[] = $val;
    }
    $sql{strlen($sql)-1} = ' ';
    $sql .= empty($this->_where) "DELETE FROM `{$this->_table}`";
    $sql .= empty($this->_where) "SELECT count(*) FROM `{$this->_table}`";
    $sql .= empty($this->_count_where) "`$col` = `$col` + $val");
    $args = array_slice(func_get_args(), 2);
    while (count($args) > 1) {
      $col = array_shift($args);
      $val = array_shift($args);
      $sets[] = "`$col` = `$col` + $val";
    }
    $sql = "UPDATE `{$this->_table}` SET ".implode(', ', $sets);
    $sql .= empty($this->_where) "UPDATE `{$this->_table}` SET `$col` = last_insert_id(`$col` + ";
    $sql .= empty($this->_where) "`{$this->_pk}` = ", $id)->select()->fetch();
  }
  /**
   * 保存数据,自动判断是新增还是更新
   * @param array $data
   * @return PDOStatement
   */
  public function save(array $data) {
    if (array_key_exists($this->_pk, $data)) {
      $pk_val = $data[$this->_pk];
      unset($data[$this->_pk]);
      return $this->where("`{$this->_pk}` = ", $pk_val)->update($data);
    } else {
      return $this->insert($data);
    }
  }
  /**
   * 获取外键数据
   * @param array $rows
   * @param string $fkey
   * @param string $field
   * @param string $key
   * @return PDOStatement
   */
  public function foreignKey(array $rows, $fkey, $field='*') {
    $ids = array(); foreach($rows as $row) { $ids[] = $row[$fkey]; }
//   $ids = array_column($rows, $fkey);
    if (empty($ids)) {
      return new PDOStatement();
    }
    return $this->where("`{$this->_pk}` in (", $ids)->select($field);
  }
}

github地址:

https://github.com/dotcoo/php/blob/master/Table/Table.php

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+MongoDB数据库操作技巧大全》、《PHP基于pdo操作数据库技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!