CodeIgniter database connection, configuration and usage

Source: Internet
Author: User
Tags php framework
This article mainly introduces the connection, configuration and usage of CodeIgniter for databases, and analyzes the connection configuration and common operation skills of CodeIgniter for databases based on the instance form, for more information about how to connect to, configure, and use CodeIgniter databases, see the following example. We will share this with you for your reference. The details are as follows:

1. database:

create database test;create table users(id int not null,name varchar(10),pwd varchar(10),email varchar(20))insert into users values(1,'shunping','shunping','aa@163.com');insert into users values(2,'shunping2','shunping2','bb@163.com');

2. I use semi QL.

Configure the database parameters in the \ CodeIgniter \ system \ application \ config \ database. php file:

$active_group = "default";$db['default']['hostname'] = "localhost";$db['default']['username'] = "postgres";$db['default']['password'] = "admin";$db['default']['database'] = "test";$db['default']['dbdriver'] = "postgre";$db['default']['dbprefix'] = "";$db['default']['active_r'] = TRUE;$db['default']['pconnect'] = FALSE;$db['default']['db_debug'] = TRUE;$db['default']['cache_on'] = FALSE;$db['default']['cachedir'] = "";$db['default']['port'] = "5432";

The content of the test file db1.php in the \ CodeIgniter \ system \ application \ controllers directory is as follows:

<? Phpclass Db1 extends Controller {function index () {$ this-> load-> database (); $ query = $ this-> db-> query ("select name, pwd, email from users "); foreach ($ query-> result () as $ row) {// returns the object array echo $ row-> name; echo $ row-> pwd; echo $ row-> email."
";}Echo" Total Result = ". $ query-> num_rows () ;}}?>

Open the browser and enter the address:

Http: /localhost: 8888/index. php/MyController/db1

OK!

I think you must have encountered a problem that cannot connect to the database. I have spent a lot of time solving this problem. now I want to help you learn about CodeIgniter, an excellent PHP framework.

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.