1:配置数据库(application/config/database.php)修改
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '数据库的用户名',
'password' => '数据库密码',
'database' => '数据表名',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
2:数据库连接($this->load->database());
3:增:
//insert //语法:$bool=$this->db->insert('表名',关联数组); $data=array( 'username'=>'mary', 'password'=>'mary',//建立一个用户名叫mary,密码为mary的数组,并传递给变量$data ); $bool=$this->db->insert('user',$data);//将$data插入数据库的user表中 var_dump($bool);//成功则返回TURE 4:删:
//delete //语法:$bool=$this->db->delete('表名',WHERE条件); $bool=$this->db->delete('user',array('id'=>3));//删除数据库.user表里id=3的用户所有信息 var_dump($bool);//成功则返回TURE 5:改:
//update $data=array( 'password'=>12345, ); $bool=$this->db->update('user',$data,array('id'=>3));//将数据库.user表里id=3的用户密码给为12345 var_dump($bool);//成功则返回TURE 6:查:
//get $res=$list=$this->db->get('user'); //var_dump($list); foreach($res->result() as $item){//利用foreach来列出所有用户名 echo $item->username; echo '
'; } 联合查询:
$this->db->join('ci_role','ci_role.role_id = ci_admin.role_id','left');
$query=$this->db->get_where('ci_admin',array('username'=>$arr['username'],'pwd'=>$pwd));
$res=$query->result();
多表联合查询:
$query = $this->db->query( "select tn.*,tnt.name,aa.sname from tx_news tn left join tx_news_type tnt on tnt.id = tn.typeid left join tx_second_type aa on tn.second_id=aa.sid where "条件" ");
$data['data'] = $query->result_array();