The examples in this article describe the connection, configuration and use of CodeIgniter for databases. Share it with everyone 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 Postgreql
Configure the database parameters in the CodeIgnitersystemapplicationconfigdatabase.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 in the CodeIgnitersystemapplicationcontrollers directory The content of the file db1.php is 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) {//返回对象数组 echo $row->name; echo $row->pwd; echo $row->email."<br>"; } echo "Total Result==".$query->num_rows(); } } ?>
Open the browser and type in the address:
http://localhost:8888/index.php/MyController/db1
ok done!
I think you must have encountered the problem of not being able to connect to the database. I spent a lot of energy to solve this problem. Now I tell you, I hope it will be helpful for you to learn CodeIgniter, an excellent PHP framework.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction to codeigniter tutorial", "CI (CodeIgniter) framework advanced tutorial", "php date and time usage summary", "php object-oriented programming" "Introductory tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
The above introduces the connection, configuration and use of CodeIgniter for the database, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.