CI 操作数据库

来源:互联网 发布:电视直播源码论坛 编辑:程序博客网 时间:2024/05/24 01:37

配置文件:application\config\database.php

//$this->load->database();//可以设置装载那个数据库
//首先需要加载数据库对象。

//$this->db;//装载到这里。

$res=$this->db->query($sql); //执行sql语句
$list=$res->result();//返回对象

$list=$res->result_array();//返回数组,内容是关联数组
$list=$res->row();//返回第一条数据,直接就是一个对象

$data['user_list']=$list;
$this->load->view('user/showusers',$data);


配置文件
C:\wamp\www\CITest05\application\config\autoload.php
$autoload['libraries'] = array('database');//自动加载database类库参数对象
C:\wamp\www\CITest05\application\config\database.php
'dbprefix' => 'ci_',
'swap_pre' => 'swap_',
//配置表前缀
//在CI中所有的sql语句都是执行query()函数
//返回受影响行数 $this->db->affected_rows()
//返回自增Id  $this->db->insert_id();

接收参数

$data[0]=$this->input->post('username');
$data[1]=$this->input->post('password');
$sql='insert into swap_user (username,password) values(?,md5(?))';
$bool=$this->db->query($sql,$data);
if($bool){
echo '受影响行数:'.$this->db->affected_rows();
echo '自增ID:'.$this->db->insert_id();
}

CI中的超级对象
$this->load->vars();
$this->load->view();
//CI中的URI属性
http://localhost/citest02/index.php?i=1
获得值:$_GET['i'];
$this->uri;
http://localhost/citest02/index.php/Welcome/index/
echo $this->uri->segment(2); //index
//从入空文件开始,控制器算第一个位置,依次类推。

原创粉丝点击