Database operations in CI

Source: Internet
Author: User

Reproduced in: http://blog.sina.com.cn/s/blog_76e7bdba01016p2p.html

The first time the database is connected in CI, enter the following statement in the constructor of the controller or model

$this->load->database ();
There is no need to repeat the connection, in that controller or model can do any number of queries. Query operation (equivalent Select) method one:

$query = $this->db->get (' sites '); Sites for Table name
 
This is a "select *" query, and the target is the site table. In other words, it is also possible to write back all the rows in the following way:

$this->db->from (' sites '); $query = $this->db->get ();
If you want to get a specific column, not all columns, do this:

$this->db->select (' url ', ' name ', ' ClientID ');//' URL ', ' name ', ' ClientID ' for column name $query = $this->db->get (' Sites ');
 
If sort:
$this->db->select (' url ', ' name ', ' ClientID ');//' URL ', ' name ', ' ClientID ' for the column name $this->db->orderby ("name", " Desc "); $query = $this->db->get (' sites ');

  

If you want to limit the number of rows returned, such as the first five results

$this->db->select (' url ', ' name ', ' ClientID ');//' URL ', ' name ', ' ClientID ' for the column name $this->db->orderby ("name", " Desc "); $this->db->limit (5); $query = $this->db->get (' sites ');
==

$this->db->where (' ClientID ', ' 1 ');  ClientID Property  "1" is a property value

  

! = situation
$this->db->where (' url! = ', ' www.mysite.com '); $this->db->where (' ID > ', ' 3 ');

  



$this->db->where (' url! = ', ' www.mysite.com '); $this->db->where (' ID > ', ' 3 ');


$this->db->where (' url! = ', ' www.mysite.com '); $this->db->orwhere (' url! = ', ' www.anothersite.com ');
Connection table
$this->db->from (' sites '); $this->db->join (' People ', ' Sites.peopleid = People.id ');
Write a full query

$this->db->select (' url ', ' name ', ' ClientID ', ' people.surname as Client '); $this->db->where (' ClientID ', ' 3 $this->db->limit (5); $this->db->from (' sites '); $this->db->join (' People ', ' Sites.clientid = People.id '); $this->db->orderby ("name", "desc"); $query = $this->db->get ();

  

Method Two:
$this->db->query ("Select ID, name, URL from sites WHERE ' type ' = ' dynamic '"); you can write the query drop condition like the following statement $condition = "client =" 3 ' and (type = ' dynamic ' OR type= ' static ') "; $this->db->where ($condition);

  

NOTE: Double quotation marks are defined for variables. Do not confuse single and double quotation marks. Show query results Add the following sentence after the query statement

$query = $this->db->get ();
 
foreach Loop:
foreach ($query->result () as $row) {   print $row->url;   Print $row->name;   Print $row->client;}
If we want only one result, it can be returned as an object, or here as an $row array

if ($query->num_rows () > 0) {   $row = $query->row_array ();    Print $row [' url '];   Print $row [' name '];   Print $row [' Client '];}
Add data (equivalent to insert) method one: First build the array, put the value of the insert into the arrays. as follows: where the URL/name/clientid/type is a data table property value

$data = Array (                ' url ' = = ' www.mynewclient.com ',                ' name ' = ' BigCo Inc ',                ' clientid                ' = ' Type ' = = ' dynamic '            ); then use $this->db->insert (' sites ', $data); Add the data to the Sites table.
method Two: use $ This->db->Set () sets each value

$this->db->set (' url ', ' www.mynewclinet.com '); $this->db->set (' name ', ' BigCo Inc '); $this->db->set (' ClientID '), $this->db->set (' type ', ' Dynamic '), $this->db->insert (' sites ');
Update (equivalent to update) to locate the record to update, and then update
$this->db->where (' id ', ' 1 '), $this->db->update (' sites ', $data), $this->db->set () mode is also available, And the new data should be the same.
 
CI provides several functions to check whether the database has successfully performed the relevant operation. Most useful:
$this->db->affected_rows ();
' 1 '- But if I'm trying to update a batch of records, I might return a larger integer. If I'm trying to insert a new record, before actually producing it,
$new _id_number = $this->db->insert_id ();
Delete (equivalent to delete)
$this->db->where (' id ', ' 2 '); $this->db->delete (' sites ');

Database operations in CI

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.