zoukankan      html  css  js  c++  java
  • php+ci对mysql进行增删改查

      php的ci是一个mvc模式的框架,本文是通过php+ci对mysql数据库进行增删改查。

    0. 首先在mysql数据库方创建数据库和数据表

    1 create database test;
    2 use test;
    3 create table user(
    4     id int(10) not null auto_increment,
    5     name varchar(50) not null,
    6     age int(10) not null,
    7     primary key (id)
    8 );
    View Code

    1. 修改database配置文件(application/config/database.php

    2. 追加models处理模块(application/models/user_model.php

     1 <?php
     2 
     3 class User_Model extends CI_Model
     4 {
     5     
     6     function __construct()
     7     {
     8         parent::__construct();
     9         // connect to the database
    10         $this->load->database();
    11     }
    12 
    13     function user_insert($arr)
    14     {
    15         $this->db->insert('user', $arr);
    16     }
    17 
    18     function user_update($id, $arr)
    19     {
    20         $this->db->where('id', $id);
    21         $this->db->update('user', $arr);
    22     }
    23 
    24     function user_delete($id)
    25     {
    26         $this->db->where('id', $id);
    27         $this->db->delete('user');
    28     }
    29 
    30     function user_select($id)
    31     {
    32         $this->db->where('id', $id);
    33         $this->db->select('*');
    34         $query = $this->db->get('user');
    35         return $query->result();
    36     }
    37 }
    View Code

    3. 追加controller处理模块(application/controllers/user.php

     1 <?php
     2 
     3 class User extends CI_Controller
     4 {
     5     public function insert()
     6     {
     7         $this->load->model('user_model');
     8         $arr = array('name'=>'aaa', 'age'=>16);
     9         $this->user_model->user_insert($arr);
    10     }
    11 
    12     public function update()
    13     {
    14         $this->load->model('user_model');
    15         $arr = array('id'=>2, 'name'=>'bbb','age'=>23);
    16         $this->user_model->user_update(2, $arr);
    17     }
    18 
    19     public function delete($id)
    20     {
    21         $this->load->model('user_model');
    22         $this->user_model->user_delete($id);
    23     }
    24 
    25     public function select()
    26     {
    27         $this->load->model('user_model');
    28         $arr = $this->user_model->user_select(1);
    29         print_r($arr);
    30     }
    31 }
    View Code

    4. 通过URL即可对数据库进行增删改查了。

    2015/08/27追记:

    学习php一周时间,基于php+ci+mysql实现了个小系统(包含登录/退出功能,记住用户登录信息,数据的增删改查,数据分页显示功能)

    小系统源代码:http://yun.baidu.com/s/1gdk35Gf#path=%252Fphp

  • 相关阅读:
    C#中IPAddress转换成整型int
    没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))
    VB.NET或C#报错:You must hava a license to use this ActiveX control.
    c#几种随机数组和数组乱序
    C#封装的websocket协议类
    VB生成条形码(EAN-13)
    VB控件间的拖放
    VB用API模拟截屏键PrintScreen
    VB读写进程的内存
    几个VB常见又内涵的错误
  • 原文地址:https://www.cnblogs.com/hezhixiong/p/4747675.html
Copyright © 2011-2022 走看看