$query = $this->db->from('user')->where(array('username' => 'BobbyPeng'))->get();
return $query->row_array();
public function findByAttributes($where = array())
{
$query = $this->db->from($this->tableName())->where($where)->get();
return $query->row_array();
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ActiveRecord extends CI_Model
{
/**
* 保存数据
*
* @param array $data 需要插入的表数据
* @return boolean 插入成功返回ID,插入失败返回false
*/
public function save($data)
{
if($this->db->set($data)->insert($this->tableName())) {
return $this->db->insert_id();
}
return FALSE;
}
/**
* Replace数据
* @param array $data
*/
public function replace($data)
{
return $this->db->replace($this->tableName(), $data);
}
/**
* 根据主键更新记录
*
* @param string $pk 主键值
* @param array $attributes 更新字段
* @param array $where 附加where条件
* @return boolean true更新成功 false更新失败
*/
public function updateByPk($pk, $attributes, $where = array())
{
$where[$this->primaryKey()] = $pk;
return $this->updateAll($attributes, $where);
}
/**
* 更新表记录
*
* @param array $attributes
* @param array $where
* @return bollean true更新成功 false更新失败
*/
public function updateAll($attributes, $where = array())
{
return $this->db->where($where)->update($this->tableName(), $attributes);
}
/**
* 根据主键删除数据
*
* @param string $pk 主键值
* @param array $where 附加删除条件
* @return boolean true删除成功 false删除失败
*/
public function deleteByPk($pk, $where = array())
{
$where[$this->primaryKey()] = $pk;
return $this->deleteAll($where);
}
/**
* 删除记录
*
* @param array $where 删除条件
* @param int $limit 删除行数
* @return boolean true删除成功 false删除失败
*/
public function deleteAll($where = array(), $limit = NULL)
{
return $this->db->delete($this->tableName(), $where, $limit);
}
/**
* 根据主键检索
*
* @param string $pk
* @param array $where 附加查询条件
* @return array 返回一维数组,未找到记录则返回空数组
*/
public function findByPk($pk, $where = array())
{
$where[$this->primaryKey()] = $pk;
$query = $this->db->from($this->tableName())->where($where)->get();
return $query->row_array();
}
/**
* 根据属性获取一行记录
* @param array $where
* @return array 返回一维数组,未找到记录则返回空数组
*/
public function findByAttributes($where = array())
{
$query = $this->db->from($this->tableName())->where($where)->limit(1)->get();
return $query->row_array();
}
/**
* 查询记录
*
* @param array $where 查询条件,可使用模糊查询,如array('name LIKE' => "pp%") array('stat >' => '1')
* @param int $limit 返回记录条数
* @param int $offset 偏移量
* @param string|array $sort 排序, 当为数组的时候 如:array('id DESC', 'report_date ASC')可以通过第二个参数来控制是否escape
* @return array 未找到记录返回空数组
*/
public function findAll($where = array(), $limit = 0, $offset = 0, $sort = NULL)
{
$this->db->from($this->tableName())->where($where);
if($sort !== NULL) {
if(is_array($sort)){
foreach($sort as $value){
$this->db->order_by($value, '', false);
}
} else {
$this->db->order_by($sort);
}
}
if($limit > 0) {
$this->db->limit($limit, $offset);
}
$query = $this->db->get();
return $query->result_array();
}
/**
* 统计满足条件的总数
*
* @param array $where 统计条件
* @return int 返回记录条数
*/
public function count($where = array())
{
return $this->db->from($this->tableName())->where($where)->count_all_results();
}
/**
* 根据SQL查询, 参数通过$param绑定
* @param string $sql 查询语句,如SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?
* @param array $param array(3, 'live', 'Rick')
* @return array 未找到记录返回空数组,找到记录返回二维数组
*/
public function query($sql, $param = array())
{
$query = $this->db->query($sql, $param);
return $query->result_array();
}
}
/* End of file ActiveRecord.php */
/* Location: ./application/core/database/ActiveRecord.php */
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'core/database/ActiveRecord.php';
class MY_Model extends ActiveRecord
{
public function __construct($group_name = '')
{
$this->initDb($group_name);
parent::__construct();
}
protected function initDb($group_name = '')
{
$db_conn_name = $this->getDbName($group_name);
$CI = & get_instance();
if(isset($CI->{$db_conn_name}) && is_object($CI->{$db_conn_name})) {
$this->db = $CI->{$db_conn_name};
} else {
$CI->{$db_conn_name} = $this->db = $this->load->database($group_name, TRUE);
}
}
private function getDbName($group_name = '')
{
if($group_name == '') {
$db_conn_name = 'db';
} else {
$db_conn_name = 'db_'.$group_name;
}
return $db_conn_name;
}
}
/* End of file MY_Model.php */
/* Location: ./application/core/MY_Model.php */
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有