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"