CodeIgniter _php instance for connection, configuration and usage of database

Source: Internet
Author: User
Tags getting started with php php database php framework codeigniter create database

This article illustrates the connection, configuration and usage of CodeIgniter to the database. Share to everyone for your reference, specific as follows:

1. Database:

Create database test;
CREATE TABLE users (
ID int not NULL,
name varchar (),
pwd varchar (),
email varchar
)
Insert into users values (1, ' shunping ', ' shunping ', ' aa@163.com ');
Insert into users values (2, ' shunping2 ', ' shunping2 ', ' bb@163.com ');

2. I'm using a postgreql.

To configure 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 contents of the test file db1.php in the \codeigniter\system\application\controllers directory are as follows:

<?php
class Db1 extends controller{
  function index () {
    $this->load->database ();
    $query = $this->db->query ("Select Name,pwd,email from Users");
    foreach ($query->result () as $row) {//Return object array
      echo $row->name;
      echo $row->pwd;
      echo $row->email. " <br> ";
    }
    echo "Total result==". $query->num_rows ();
>

Open Browser Typing address:

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

OK, it's done!

I think you must have encountered the problem of not even database, I spent a lot of energy to solve this problem, now tell you, hope to learn codeigniter this excellent PHP framework to help.

More interested in PHP related content readers can view the site topics: "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP date and Time usage summary", "PHP object-oriented Program Design Primer", "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"

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.