CodeIgniter How to connect, configure and use database

Source: Internet
Author: User
Tags getting started with php php framework codeigniter
This paper describes the connection, configuration and usage of CodeIgniter for database. Share to everyone for your reference, as follows:

1. Database:

Create Database Test;create table users (id int not null,name varchar), pwd varchar ($), email varchar) INSERT INTO use RS 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";

Test the file db1.php content in the \codeigniter\system\application\controllers directory 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 an array of objects      echo $row->name;      echo $row->pwd;      echo $row->email. "
"; } echo "Total result==". $query->num_rows (); }}? >

Open the browser-typed address:

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

OK, fix it!

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

More readers interested in PHP related content can view this site topic: "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction tutorial", "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

The above describes the CodeIgniter for the database connection, configuration and use of the method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.