随笔 - 33  文章 - 1  评论 - 17  阅读 - 12万

CI(CodeIgniter)框架中的增删改查操作

 

我们创建一个模型( 项目目录/models/),请注意:模型名与文件名相同且必须继承数据核心类CI_Model,同时重载父类中的构造方法

 

CodeIgniter的数据函数类在 \models\User_model.php

连接数据库:$this->load->database();

1 class  User_model extends CI_Model{
2 
3   public  function  __construct(){
4     parent::__construct(); 
5     $this->load->database();
6   }
7 }

 

 插入数据

1 public  function Add(){
2     $data=array(
3       'fName'=>'test',
4       'fPassword'=>'test',
5       'fAddress'=>'address ' 
6         );
7   return $this->db->insert("User",$data);
8 }

 

 

更新数据

复制代码
1 public function  Update($id){
2         $data=array(
3                   'fName'=>'ddd',
4                   'fPassword'=>'tesdddt',
5                   'fAddress'=>'address ' 
6                           );
7         $this->db->where('fId',$id);  
8         return $this->db->update("User",$data);
9 }
复制代码

 

 

删除数据

1 public  function Delete($id){
2     $this->db->where('fId',$id);  
3     return $this->db->delete("User");
4 }

 

查询全部数据

1 public  function  GetAll(){
2     $query =$this->db->get("User");
3     return  $query->result_array();
4 }

 

查询指定数据:

1 public  function GetUser($id){
2      $this->db->where('fId',$id);  
3      $this->db->select('*'); 
4      $query= $this->db->get('User'); 
5      return$query->result();
6 }

 

posted on   守护者  阅读(9082)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示