codeigniter db操作方法
- 链接数据库
- ——-
- $this->load->database();//手动连接数据库
- //连接多数据库
- $DB1 = $this->load->database(‘group_one’, TRUE);
- $DB2 = $this->load->database(‘group_two’, TRUE);
- —————————————————–
- 查询
- ——-
- //参数绑定形式
- $sql = “SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?”;
- $this->db->query($sql, array(3, ‘live’, ‘Rick’));
- //多结果标准查询
- $query = $this->db->query($sql); //自定义
- $query = $this->db->get(‘tablename’); //便捷形式,相当于:SELECT * FROM tablename
- $query = $this->db->get(‘tablename’, 10, 20); // 相当于: SELECT * FROM tablename LIMIT 20, 10
- $query->result() //对象形式
- $query->result_array() //数组形式
- $query->num_rows() //总条数
- $query->num_fields() //字段数
- //单结果标准查询
- $row = $query->row(); //对象形式
- $row = $query->row_array(); //数组形式
- —————————————————–
- 插入
- ——-
- $data = array(
- ‘title’ => $title,
- ‘name’ => $name
- );
- $this->db->insert(‘tablename’, $data); //便捷插入
- $this->db->insert_string(‘tablename’, $data); //便捷插入
- $this->db->insert_id() //刚插入的id
- $this->db->affected_rows() //影响的行数(update,insert)
- —————————————————–
- 更新
- ——-
- $data = array(
- ‘name’ => $name,
- ‘email’ => $email
- );
- $where = “id = 1″;
- $this->db->update(‘tablename’, $data);
- $this->db->update_string(‘tablename’, $data, $where);
- —————————————————–
- 删除
- ——-
- $array = array(
- ‘name’ => $name,
- ‘title’ => $title
- );
- $this->db->delete(‘tablename’, $array);
- // Produces:
- // “DELETE FROM tablename WHERE name = ‘$name’ AND title = ‘$title’”
- $this->db->truncate(‘tablename’); //清空表
- // Produce: TRUNCATE tablename
- —————————————————–
- (where)
- ——-
- $array = array(
- ‘name’ => $name,
- ‘title’ => $title
- );
- $this->db->where($array);
- // Produces: “WHERE name = ‘$name’ AND title = ‘$title’”
- —————————————————–
- $this->db->count_all(‘tablename’); //表中记录总行数
- —————————————————–
- $query->free_result() //释放资源
codeigniter db操作方法的更多相关文章
- kohana 简单使用
声明:基于公司使用的 Kohana 框架写的,不确定是否适用于原生 Kohana 附:Kohana 3 中文手册,传送门:http://www.lampblog.net/kohana3%E4%BD%B ...
- 黄聪:Discuz!X/数据库操作方法、DB::table、C::t
函数 功能 DB::table($tablename) 获取正确带前缀的表名,转换数据库句柄, DB::delete($tablename, 条件,条数限制) 删除表中的数据 DB::insert($ ...
- Codeigniter开发技巧:连接多个数据库(可实现DB读写分离)
在开发中,我们有时候会遇到在同一程序中链接多个数据库的需求,这对Codeigniter框架来说是很简单的,我们只需要在 database.php文件中配置少许参数即可. 默认情况下,CI配置的是链接一 ...
- 反射 + 抽象工厂模式切换DB数据源(附Demo)
首先,设计模式的文章源自于程杰的<大话设计模式>这本书,这本书个人感觉很适合我,看着不累,能够安安心心的阅读学习.在这里十分感谢程杰的这本书,我博文中的例子会根据书上的例子来.为了不侵犯这 ...
- CI框架源码阅读笔记4 引导文件CodeIgniter.php
到了这里,终于进入CI框架的核心了.既然是“引导”文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http://you.host.c ...
- asp.net mvc 控制器中操作方法重载问题 解决
Controllers: public ActionResult Index() { return View(db.GuestBooks.ToList()); } // // GET: /Guest2 ...
- 常用的PHP数据库操作方法(MYSQL版)
常用的PHP数据库操作方法(MYSQL版) 作者: 字体:[增加 减小] 类型:转载 时间:2011-06-08 最近一直在折腾自己的网站首页,写的大部分PHP脚本都要用到和MYSQL数据库相关的 ...
- 【转】CodeIgniter配置之database
CodeIgniter的数据库配置文件位于application/config/database.php, 该文件中定义了$db的二维数组,参考文件如下: 1 2 3 4 5 6 7 8 9 10 1 ...
- Codeigniter MongoDB类库
安装方法:1.将mongodb.php 放到config目录2.将Mongo_db.php放到library目录 使用方法: $this->mongo_db->where_gte('age ...
随机推荐
- linux的5个查找命令_转
转自:http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 在Linux中 ...
- 无线AP_H3C WA2620i-AGN配置
设置Console口登录AP设备的用户进行密码认证: <Sysname> system-view //进入系统视图 [WA2620i-AGN]user-interface console ...
- Django 学习
urls.py 路由系统 from django.conf.urls import url,includefrom django.contrib import adminfrom web import ...
- 安装Cocoapods(Pods 管理iOS 第三方库)
安装 可先检测Mac电脑是否安装Pods.打开控制台: $ which pod 如果安装,结果如下;如果没有安装,控制台无反应. /usr/bin/pod 如果没有安装, 以下命令安装之. $ sud ...
- my.cnf详解
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock basedir ...
- ORACLE 11G内存管理方式
SGA包含的组件: 组件名 说明 参数 buffer cache 存放从数据文件中读取的数据拷贝,所有用户之间是可以共享的 db_cache_size db_keep_cache_size db_re ...
- Oracle 11g RAC INS-06006 Passwordless SSH connectivity not set up between the following node(s)
安装11g RAC的grid时,在Test互信的时候报错INS-06006 Passwordless SSH connectivity not set up between the following ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(1)
在本章中,我们希望将我们的注意力集中在写可扩展,多主,同步,对称和PostgreSQL的称为Postgres-XC(PostgreSQL eXtensible Cluster)的透明复制方案.该项目的 ...
- 强连通(hdu4635)最多增加几条单向边后满足最终的图形不是强连通
Strongly connected Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...