Codeigniter操作数据库表的优化写法总结
程序员文章站
2023-12-10 23:12:22
用codeigniter也有一段时间了,一直没有做什么总结。现在总结一些codeigniter操作数据库表的优化写法,虽说不全,但是也确实可以帮助那些刚刚上手ci的同学。...
用codeigniter也有一段时间了,一直没有做什么总结。现在总结一些codeigniter操作数据库表的优化写法,虽说不全,但是也确实可以帮助那些刚刚上手ci的同学。
链接数据库
复制代码 代码如下:
$this->load->database();//手动连接数据库
//连接多数据库
$db1 = $this->load->database('group_one', true);
$db2 = $this->load->database('group_two', true);
//连接多数据库
$db1 = $this->load->database('group_one', true);
$db2 = $this->load->database('group_two', true);
查询
复制代码 代码如下:
//参数绑定形式
$sql = "select * from some_table where id = ? and status = ? and author = ?";
$this->db->query($sql, array(3, 'live', 'rick'));
//多结果标准查询
$query = $this->db->query($sql); //自定义
$query = $this->db->get('tablename'); //便捷形式,相当于:select * from tablename
$query = $this->db->get('tablename', 10, 20); // 相当于: select * from tablename limit 20, 10
$query->result() //对象形式
$query->result_array() //数组形式
/*
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
*/
$query->num_rows() //总条数
$query->num_fields() //字段数
//单结果标准查询
$row = $query->row(); //对象形式
$row = $query->row_array(); //数组形式
/*
$row = $query->row_array();
echo $row['name'];
*/
$sql = "select * from some_table where id = ? and status = ? and author = ?";
$this->db->query($sql, array(3, 'live', 'rick'));
//多结果标准查询
$query = $this->db->query($sql); //自定义
$query = $this->db->get('tablename'); //便捷形式,相当于:select * from tablename
$query = $this->db->get('tablename', 10, 20); // 相当于: select * from tablename limit 20, 10
$query->result() //对象形式
$query->result_array() //数组形式
/*
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->email;
}
*/
$query->num_rows() //总条数
$query->num_fields() //字段数
//单结果标准查询
$row = $query->row(); //对象形式
$row = $query->row_array(); //数组形式
/*
$row = $query->row_array();
echo $row['name'];
*/
插入
复制代码 代码如下:
$data = array(
'title' => $title,
'name' => $name
);
$this->db->insert('tablename', $data); //便捷插入
$this->db->insert_string('tablename', $data); //便捷插入
$this->db->insert_id() //刚插入的id
$this->db->affected_rows() //影响的行数(update,insert)
'title' => $title,
'name' => $name
);
$this->db->insert('tablename', $data); //便捷插入
$this->db->insert_string('tablename', $data); //便捷插入
$this->db->insert_id() //刚插入的id
$this->db->affected_rows() //影响的行数(update,insert)
更新
复制代码 代码如下:
$data = array(
'name' => $name,
'email' => $email
);
$where = "id = 1";
$this->db->update('tablename', $data);
$this->db->update_string('tablename', $data, $where);
'name' => $name,
'email' => $email
);
$where = "id = 1";
$this->db->update('tablename', $data);
$this->db->update_string('tablename', $data, $where);
删除
复制代码 代码如下:
$array = array(
'name' => $name,
'title' => $title
);
$this->db->delete('tablename', $array);
// produces:
// "delete from tablename where name = '$name' and title = "$title""
$this->db->truncate('tablename'); //清空表
// produce: truncate tablename
-----------------------------------------------------
(where)
-------
$array = array(
'name' => $name,
'title' => $title
);
$this->db->where($array);
// produces: "where name = '$name' and title = "$title""
-----------------------------------------------------
$this->db->count_all('tablename'); //表中记录总行数
-----------------------------------------------------
$query->free_result() //释放资源
'name' => $name,
'title' => $title
);
$this->db->delete('tablename', $array);
// produces:
// "delete from tablename where name = '$name' and title = "$title""
$this->db->truncate('tablename'); //清空表
// produce: truncate tablename
-----------------------------------------------------
(where)
-------
$array = array(
'name' => $name,
'title' => $title
);
$this->db->where($array);
// produces: "where name = '$name' and title = "$title""
-----------------------------------------------------
$this->db->count_all('tablename'); //表中记录总行数
-----------------------------------------------------
$query->free_result() //释放资源