1、配置

在config->database.php文件里
PHP的CI框架使用MySQL

2、查询语句select

PHP的CI框架使用MySQL
$this->db->select(‘*’)
->from(‘goods’) //goods是表名
->get()->result(); //*是查询goods表里的所有属性,如果只想查询一个表里的部分属性,可以使用 表名.属性名 的写法

关联多个表使用 join 关键字

$this->db->select(‘*’)
->from(‘goods’)
->join(‘表名’)
->get()->result();

一般来说关联的表最好不超过3个.

可以对表重命名,
$this->db->select(‘A.id,A.name,B.shelves_name,C.id’,’) //查询goods表的id,name,shelves的shelves_name,borrow表的id
->from(‘goods A’) //把goods表命名为A
->join(‘shelves B’) // shelves表命名为B
->join(‘borrow C’) //borrow表命名为 C
->get()->result();

条件查询where

$this->db->select(‘A.id,A.name,B.shelves_name,C.id’,’)
->from(‘goods A’)
->join(‘shelves B’)
->join(‘borrow C’)
->where(‘A.name’,‘zhangsan’)
->or_where(‘A.name’,‘lisi’)//这里查询goods表name为zhangsan和lisi的数据
->get()->result();

3、insert插入语句

方法一:建立一个数组,把数据放入数组里
PHP的CI框架使用MySQL
方法二:使用set()
$this->db->set(‘goods_id’, 1);
$this->db->set(‘user_id’, 1);
$this->db->set(‘borrowing_time’, ‘date(‘Y-m-d H:i:s’)’);
$this->db->set(‘borrow_num’, ‘33’);
$this->db->insert(‘表名’);

4、update更新语句

$this->db->update(‘表名’, $list, [‘id’ => 1])//更新id=1的数据,更新的内容放在数组list里,和insert写法类似

5、delete删除语句

$this->db->where(‘id’, ‘2’);
$this->db->delete(‘表名’);

相关文章:

  • 2022-12-23
  • 2021-10-08
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2021-09-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-13
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案