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"