Home > Article > Backend Development > CodeIgniter for database connection, configuration and usage, codeigniter database_PHP tutorial
This article describes the examples of CodeIgniter's connection, configuration and use methods 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 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";
The content of the test file db1.php in the CodeIgnitersystemapplicationcontrollers directory 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 your 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 program" Design introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"