CodeIgniter Framework Database Basic operations

Source: Internet
Author: User
Tags md5 encryption php foreach codeigniter
The basic operation of CodeIgniter framework database is described in this paper. Share to everyone for your reference, as follows:

Now, first, the CI framework is now in its own server directory and configured config/config.php

$config [' base_url '] = ' http://localhost:90/CI/';

Then come down to configure the database in config/databases.php I do the exercises configured as follows

$db [' Default '] [' hostname '] = ' localhost '; $db [' Default '] [' username '] = ' root '; $db [' Default '] [' password '] = ' root '; $db [' Default '] [' database '] = ' demo '; $db [' Default '] [' dbdriver '] = ' mysql ';

Other now novice do not have to create a database and a user table, this in my source package has you can directly import it, but the premise you want to create a demo database

The Reg class code is as follows

<?php/*************************************** * Basic operating practices for user registration modules and databases * 17:44 2013.2.18 * @Author sdeep Wang ************ /class Reg extends ci_controller{function __construct () {//each time this function must be written is a method that inherits the parent class parent::__    Construct (); $this->load->database ();//This is the way to connect to the database, and the benefits of this are as long as the method is called to connect to the database} function index () {$this->load->view (' reg _view ');//This is the view used to show the equivalent of display in Smarty function Reg_insert () {$data [' name '] = $this->input->post (' name ');    /This means getting the value of the post array and assigning a heart array $data [' sex '] = $this->input->post (' sex ');    $data [' age '] = $this->input->post (' age '); $data [' pwd '] = MD5 ($this->input->post (' pwd '));//Here is a MD5 encryption only to demonstrate $data [' email '] = $this->input->post ('    Email '); $this->db->insert (' user ', $data);//This is a database operation insert operation redirect ('/reg/reg_select/', ' Refresh ');// This is a jump function is a URL helper function inside a method} function Reg_select () {//This query database method $this->db->select (' id,name,sex,age,email ');/ This is the field that the query wants to display, but it's not like I wrote it the first time. $THIS-&GT;db->select (' id ', ' name ', ' sex ', ' age ', ' email '); $data [' query '] = $this->db->get (' user ');//This is the acquisition of data (if you write the same as the first time I can only take a field) $this->load->view (' Select _view ', $data);//Here is what view is called and assigns data to the specified view to display} function Reg_delete () {//delete data operation $id = $this->input->get (' id ');//This is get g The values that et passes over $this->db->where (' id ', $id);//This is quite important to make a where condition, and if there is no this you may have emptied the table data $this->db->delete (' user ' );//delete the specified ID data redirect ('/reg/reg_select/', ' Refresh ');//Same as Jump} function Reg_update () {//operation with new data $data [' id '] = $this- >input->get (' id ');//Get pass-through ID $this->load->view (' Update_view ', $data);//Same as Call view assignment data} function reg_ Com_update () {//This is true with the new data operation method $id = $this->input->post (' id ');//the ID value in post is obtained $data = array (//wrap the value of the post array into the new array          For the following new operation with ' name ' = $this->input->post (' name '), ' pwd ' =>md5 ($this->input->post (' pwd ')),    ' Email ' = $this->input->post (' email ')); if (!empty ($id) && (count ($data) > 1)) {//Determine if the ID value is passed over and determine if the encapsulated array has elements $this->db->where (' id ', $id);//Ibid. prepare the Where Condition $this->db->update (' u Ser ', $data);//With new operation} redirect ('/reg/reg_select/', ' Refresh ');//ditto Jump}}?>

The

View code is as follows

The second view code is as follows

As follows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.