CIPHP framework, how to use database connection classes, and how to add, delete, modify, and query database links

Source: Internet
Author: User
It is a long journey to study the CI framework. we hope that new users will not worry about it. let's study it together. CI is called Agile development. it has many advantages. it is said that the research on the CI framework of databases has been a long process. we hope new users will not worry about it. let's learn it together. CI is called Agile development. it has many advantages. it means that database connection and database operations are flexible. below we will show you the most basic code and operations for dealing with databases.
Initialize database class

The following code loads and initializes the database class based on your code:

$ This-> load-> database ();

Once loaded, you can use it anywhere like this:

Note: If all your pages require database class initialization, you can make it automatically loaded.

Standard multi-result query (object form)

$ Query = $ this-> db-> query ('select name, title, email FROM my_table ');

Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
Echo $ row-> name;
Echo $ row-> email;
}

Echo 'total Results: '. $ query-> num_rows ();


The result () function above returnsObject. Example: $ row-> title

Standard multi-result query (in array format)

$ Query = $ this-> db-> query ('select name, title, email FROM my_table ');

Foreach ($ query-> result_array () as $ row)
{
Echo $ row ['title'];
Echo $ row ['name'];
Echo $ row ['email '];
}


The above result_array () function returns an array with the lower mark. Example: $ row ['title']

Test query results

If your query may not return results, we recommend that you use the num_rows () function to test:

$ Query = $ this-> db-> query ("your query ");

If ($ query-> num_rows ()> 0)
{
Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
Echo $ row-> name;
Echo $ row-> body;
}
}


Standard single-result query (object form)

$ Query = $ this-> db-> query ('select name FROM my_table LIMIT 1 ');

$ Row = $ query-> row ();
Echo $ row-> name;


The row () function above returnsObject. Example: $ row-> name

Standard single-result query (in array format)

$ Query = $ this-> db-> query ('select name FROM my_table LIMIT 1 ');

$ Row = $ query-> row_array ();
Echo $ row ['name'];


The row_array () function above returnsArray. Example: $ row ['name']

Standard insert)

$ SQL = "INSERT INTO mytable (title, name)
VALUES (". $ this-> db-> escape ($ title).", ". $ this-> db-> escape ($ name ).")";

$ This-> db-> query ($ SQL );

Echo $ this-> db-> affected_rows ();


Quick Query

The quick query class provides us with a way to quickly obtain data:

$ Query = $ this-> db-> get ('Table _ name ');

Foreach ($ query-> result () as $ row)
{
Echo $ row-> title;
}


The get () function above returns all results in the data table. The quick query class provides quick functions for all database operations.

Insert)

$ Data = array (
'Title' => $ title,
'Name' => $ name,
'Date' => $ date
);

$ This-> db-> insert ('mytable', $ data );

// Produces: insert into mytable (title, name, date) VALUES ('{$ title}', '{$ name}', '{$ date }')

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.