Heim  >  Artikel  >  Backend-Entwicklung  >  CodeIgniter针对数据库的连接、配置及使用方法,codeigniter数据库_PHP教程

CodeIgniter针对数据库的连接、配置及使用方法,codeigniter数据库_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:57:52790Durchsuche

CodeIgniter针对数据库的连接、配置及使用方法,codeigniter数据库

本文实例讲述了CodeIgniter针对数据库的连接、配置及使用方法。分享给大家供大家参考,具体如下:

1. 数据库:

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. 我用的是Postgreql
 
在\CodeIgniter\system\application\config\database.php文件中配置数据库参数:

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

在\CodeIgniter\system\application\controllers目录下测试文件db1.php内容如下:

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

打开浏览器敲入地址:

http://localhost:8888/index.php/MyController/db1

ok搞定!

我想大家肯定也遇到过连不上数据库的问题,我花费了好些精力才解决这个问题,现在告诉大家,希望对大家学习CodeIgniter这个优秀的PHP框架有所帮助。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

您可能感兴趣的文章:

  • CodeIgniter中使用Smarty3基本配置
  • CodeIgniter辅助之第三方类库third_party用法分析
  • CodeIgniter框架数据库事务处理的设计缺陷和解决方案
  • 新浪SAE云平台下使用codeigniter的数据库配置
  • Codeigniter整合Tank Auth权限类库详解
  • 使用CodeIgniter的类库做图片上传
  • Codeigniter操作数据库表的优化写法总结
  • codeigniter数据库操作函数汇总
  • 让CodeIgniter数据库缓存自动过期的处理的方法
  • codeigniter自带数据库类使用方法说明
  • Codeigniter中集成smarty和adodb的方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1106127.htmlTechArticleCodeIgniter针对数据库的连接、配置及使用方法,codeigniter数据库 本文实例讲述了CodeIgniter针对数据库的连接、配置及使用方法。分享给大家供...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn