• 链接数据库
  • ——-
  • $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操作方法的更多相关文章

  1. kohana 简单使用

    声明:基于公司使用的 Kohana 框架写的,不确定是否适用于原生 Kohana 附:Kohana 3 中文手册,传送门:http://www.lampblog.net/kohana3%E4%BD%B ...

  2. 黄聪:Discuz!X/数据库操作方法、DB::table、C::t

    函数 功能 DB::table($tablename) 获取正确带前缀的表名,转换数据库句柄, DB::delete($tablename, 条件,条数限制) 删除表中的数据 DB::insert($ ...

  3. Codeigniter开发技巧:连接多个数据库(可实现DB读写分离)

    在开发中,我们有时候会遇到在同一程序中链接多个数据库的需求,这对Codeigniter框架来说是很简单的,我们只需要在 database.php文件中配置少许参数即可. 默认情况下,CI配置的是链接一 ...

  4. 反射 + 抽象工厂模式切换DB数据源(附Demo)

    首先,设计模式的文章源自于程杰的<大话设计模式>这本书,这本书个人感觉很适合我,看着不累,能够安安心心的阅读学习.在这里十分感谢程杰的这本书,我博文中的例子会根据书上的例子来.为了不侵犯这 ...

  5. CI框架源码阅读笔记4 引导文件CodeIgniter.php

    到了这里,终于进入CI框架的核心了.既然是“引导”文件,那么就是对用户的请求.参数等做相应的导向,让用户请求和数据流按照正确的线路各就各位.例如,用户的请求url: http://you.host.c ...

  6. asp.net mvc 控制器中操作方法重载问题 解决

    Controllers: public ActionResult Index() { return View(db.GuestBooks.ToList()); } // // GET: /Guest2 ...

  7. 常用的PHP数据库操作方法(MYSQL版)

    常用的PHP数据库操作方法(MYSQL版) 作者: 字体:[增加 减小] 类型:转载 时间:2011-06-08   最近一直在折腾自己的网站首页,写的大部分PHP脚本都要用到和MYSQL数据库相关的 ...

  8. 【转】CodeIgniter配置之database

    CodeIgniter的数据库配置文件位于application/config/database.php, 该文件中定义了$db的二维数组,参考文件如下: 1 2 3 4 5 6 7 8 9 10 1 ...

  9. Codeigniter MongoDB类库

    安装方法:1.将mongodb.php 放到config目录2.将Mongo_db.php放到library目录 使用方法: $this->mongo_db->where_gte('age ...

随机推荐

  1. EBS R12.2快速安装前没有配置Global Inventory报错

    EBS R12.2快速安装前没有配置Global Inventory,导致验证时"file systems"这一项没有通过,被标记了"X": (本图其它两个验证 ...

  2. Troubleshooting JDK

    收集整理下JDK自带的关于 Troubleshooting 的文档 Java 2 Platform, Standard Edition 5.0 Troubleshooting and Diagnost ...

  3. Leetcode: Guess Number Higher or Lower

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  4. java List 简单使用

    Student类 class Student{ String name; String pwd; public Student(){} public Student(String name, Stri ...

  5. codevs 1202 求和

    http://codevs.cn/problem/1202/ 1202 求和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解       题目描述 D ...

  6. shell学习笔记(1)-变量

    1.shell中的变量可以自定义,shell中使用变量时用$ name="shero"echo "hi ${name}" root@shero-virtual- ...

  7. 查看真机的系统版本sdk

    1.adb devices 确保连接上了真机 2.adb shell 进入android系统 3.进入system目录下 4.查看build.prop文件 cat build.prop

  8. HDU 3037 Saving Beans(Lucas定理模板题)

    Problem Description Although winter is far away, squirrels have to work day and night to save beans. ...

  9. Spring容器中的Bean

    一,配置合作者的Bean Bean设置的属性值是容器中的另一个Bean实力,使用<ref.../>元素,可制定一个bean属性,该属性用于指定容器中其他Bean实例的id属性 <be ...

  10. yii2表关联实例

    yii2表关联 1.两张表关联,以“商品表关联品牌表”为例 控制器中: $goods_model=new Goods(); $goods_info=$goods_model::find()->j ...