设为首页 加入收藏

TOP

php 数据库类(二)
2017-10-10 12:01:59 】 浏览:7238
Tags:php 数据库
ction setLimit( $limit ) { $this->limit = $limit; } /** * 获取条数 */ function getLimit() { return $this->limit; } /** * 根据主键获取 */ function getInfoByPk() { $sql = "select {$this->getFields()} " ."from {$this->getTable()} " ."where {$this->getPrimaryKeyField()}={$this->getPk()}"; return $this->query_one($sql); } /** * 根据where条件获取一条信息 */ function getOneByWhere() { $sql = "SELECT {$this->getFields()} " . "FROM {$this->getTable()} " . "WHERE {$this->getWhere()}"; return $this->query_one($sql); } /** * 根据where条件获取数组列表 */ function getListByWhere() { $sql = "SELECT "; $sql .= "{$this->getFields()} " . "FROM {$this->getTable()} "; if ( $this->getWhere() != null ) { $sql .= "WHERE {$this->getWhere()} "; } if ( $this->getGroupby() != null ) { $sql .= "GROUP BY {$this->getGroupby()} "; } if ( $this->getOrderby() != null ) { $sql .= "ORDER BY {$this->getOrderby()} "; } if ( $this->getLimit() != null ) { $sql .= "LIMIT {$this->getLimit()}"; } return $this->query_all($sql); } /** * 根据where获取count */ function getCountByWhere() { $sql_count = "SELECT COUNT(*) AS total FROM {$this->getTable()} "; if ( $this->getWhere() != null ) { $sql_count .= "WHERE " . $this->getWhere(); } return $this->query_scalar($sql_count); } /** * 根据主键更新 */ function updateByPk($entity) { $this->setFieldValues($entity); $sql = "UPDATE {$this->getTable()} SET "; foreach ($this->getFieldValues() as $key => $one){ if ($one != NULL){ $sql .= "$key='$one',"; } } $sql = rtrim($sql, ','); $sql .= " WHERE {$this->getPrimaryKeyField()}='{$this->getPk()}'"; return $this->execute($sql); } /** * 根据WHERE更新 */ function updateByWhere($entity) { $this->setFieldValues($entity); $sql = "UPDATE {$this->getTable()} SET "; foreach ($this->getFieldValues() as $key => $one){ if ($one != NULL){ $sql .= "$key='$one',"; } } $sql = rtrim($sql, ','); $sql .= " {$this->getWhere()}"; return $this->execute($sql); } /** * 根据WHERE更新 */ function insert_table($entity) { $this->setFieldValues($entity); $sql_values = ''; $sql = "INSERT INTO {$this->getTable()} ("; foreach ($this->getFieldValues() as $key => $one){ if ($one != NULL){ $sql .= "$key,"; $sql_values .= "'$one',"; } } $sql = rtrim($sql, ',').") VALUES (".rtrim($sql_values, ',').")"; return $this->execute($sql); } //-------------------------Yii2 base---------------------- //-------------------------Yii2 base---------------------- //-------------------------Yii2 base---------------------- //* @author nike@youfumama.com //* @date 2017-03-08 //* 可扩展 /** * 只获取一行,如果该查询没有结果则返回 false * @param type $sql * @return mix */ private function query_one($sql) { return $this->_getDb()->createCommand($sql)->queryOne(); } /** * 返回所有数组,如果该查询没有结果则返回空数组 * @param type $sql * @return type */ private function query_all($sql) { return $this->_getDb()->createCommand($sql)->queryAll(); } /** * 返回一个
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇单例模式 俗称单例3步曲+1曲 下一篇PHP函数之自定义函数

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目