Home > Backend Development > PHP Tutorial > CodeIgniter database connection, configuration and usage

CodeIgniter database connection, configuration and usage

WBOY
Release: 2016-07-29 09:00:25
Original
984 people have browsed it

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');

Copy after login

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";

Copy after login

Test in the CodeIgnitersystemapplicationcontrollers directory The content of the file db1.php is as follows:

<&#63;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();
  }
}
?>

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template