博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeIgniter 新手配置用户指南-数据库配置篇
阅读量:5880 次
发布时间:2019-06-19

本文共 4155 字,大约阅读时间需要 13 分钟。

前沿:公司使用的CI框架,以前上学的时候没有接触过,对国外的优秀框架,几乎都没了解过,(英语是硬伤)。最近在接触学习CI框架,把学习成果,分享下:

 

1.下载最新的CI框架:

http://codeigniter.org.cn/download

2.解压到web根目录,

/Users/jason/project/ci/

3.目录文件列表

Ray-2:ci ray$ ll

total 24
drwxr-xr-x@ 7 ray staff 238 10 8 2012 .
drwxr-xr-x 7 root admin 238 6 14 09:45 ..
drwxr-xr-x@ 17 ray staff 578 10 8 2012 application
-rw-r--r--@ 1 ray staff 6357 10 8 2012 index.php
-rw-r--r--@ 1 ray staff 2496 10 8 2012 license.txt
drwxr-xr-x@ 10 ray staff 340 10 8 2012 system
drwxr-xr-x@ 17 ray staff 578 10 8 2012 user_guide

4.配置虚拟主机、域名映射本机IP

5.通过浏览器访问域名:

Welcome to CodeIgniter!

The page you are looking at is being generated dynamically by CodeIgniter.

If you would like to edit this page you'll find it located at:

application/views/welcome_message.php

The corresponding controller for this page is found at:

application/controllers/welcome.php

If you are exploring CodeIgniter for the very first time, you should start by reading the .

$db['default']['hostname'] = 'localhost';

$db['default']['username'] = 'root';
$db['default']['password'] = '123456';
$db['default']['database'] = 'myci';
$db['default']['dbdriver'] = 'mysql';

+++++++++++ 前提 数据库的地址、用户名、密码、数据库都是已存在且正确的,真是环境不要使用管理员链接数据库即可 ++++++++++++++++++++

8.创建model层的文件

/Users/jason/project/ci/application/models

vim testModel.php

<?php

class testModel extends CI_Model{
public $db;

public function __construct(){

$this->db = $this->load->database('default', TRUE);
// parent::__construct();

}

public function Insertdata($name){

return $this->db->insert('test',array('name'=>$name));
}

public function getdata(){

return $this->db->query('select * from test limit 20')->result_array();
}

}

?>

注意:model文件的名字要和类名相同。

9.修改controller层的文件

Ray-2:controllers ray$ ls

index.html welcome.php
Ray-2:controllers ray$ pwd
/Users/jason/project/ci/application/controllers
Ray-2:controllers ray$vim welcome.php

-------------------------------------

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**

* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
*/
public function __construct(){
parent::__construct();
$this->load->model('testModel');
}
public function index()
{
// $this->load->model('testmodel');
$data=$this->testModel->getdata();
$this->load->view('welcome_message',array('data'=>$data));
}
public function insert($name){

// $this->load->model('testmodel');

$this->testModel->Insertdata($name);
}
}

/* End of file welcome.php */

/* Location: ./application/controllers/welcome.php */

10.修改view层的文件

Ray-2:views ray$ ls

index.html welcome_message.php
Ray-2:views ray$ pwd
/Users/jason/project/ci/application/views

在默认的试图文件里面添加一下内容

<div id="container">

<h1>Welcome to CodeIgniter!</h1>

<div id="body">

<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>

<p>If you would like to edit this page you'll find it located at:</p>

<code>application/views/welcome_message.php</code>

<p>The corresponding controller for this page is found at:</p>

<code>application/controllers/welcome.php</code>

<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>

</div>

<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>

</div>
<div>
<?php echo '<pre>';var_dump($data);?>
</div>
</body>
</html>

11.数据库结构

test 表

字段:

id int 主键,自增

name varchar 64

12.添加测试数据

在浏览器输入 http://www.myci.com/index.php/welcome/insert/jason  回车即可

13 查看数据是否插入成功?

在浏览器输入 http://www.myci.com/ 回车。

结果已成功插入。 OK

 

转载于:https://www.cnblogs.com/zhongbin/archive/2013/06/14/3135197.html

你可能感兴趣的文章
安装gulp及相关插件
查看>>
如何在Linux用chmod来修改所有子目录中的文件属性?
查看>>
Applet
查看>>
高并发环境下,Redisson实现redis分布式锁
查看>>
关于浏览器的cookie
查看>>
Hyper-V 2016 系列教程30 机房温度远程监控方案
查看>>
.Net 通过MySQLDriverCS操作MySQL
查看>>
JS Cookie
查看>>
ubuntu Unable to locate package sysv-rc-conf
查看>>
笔记:认识.NET平台
查看>>
cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)
查看>>
【吉光片羽】短信验证
查看>>
MacBook如何用Parallels Desktop安装windows7/8
查看>>
gitlab 完整部署实例
查看>>
GNS关于IPS&ASA&PIX&Junos的配置
查看>>
影响企业信息化成败的几点因素
查看>>
SCCM 2016 配置管理系列(Part8)
查看>>
struts中的xwork源码下载地址
查看>>
ABP理论学习之仓储
查看>>
我的友情链接
查看>>