ci框架数据库操作快查

首先获取数据库对象;

$db=$this->load->database();
//多数据库情况

$db1 = $this->load->database('default',TRUE);//注意第一个参数:值与配置文件中的第一个索引对应
$db2 = $this->load->database('additional', TRUE);//注意第一个参数:值与配置文件中的第一个索引对应

 1. 多结果标准查询

//对象形式
$query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() as $row) { echo $row->title; echo $row->name; echo $row->email; } echo 'Total Results: ' . $query->num_rows();
//数组形式
$query = $this->db->query('SELECT name, title, email FROM my_table');

foreach ($query->result_array() as $row)
{
    echo $row['title'];
    echo $row['name'];
    echo $row['email'];
}
//上面所有的这些方法都会把所有的结果加载到内存里(预读取), 当处理大结果集时最好使用 unbuffered_row() 方法。  
$query = $this->db->query("YOUR QUERY");

while ($row = $query->unbuffered_row())
{
    echo $row->title;
    echo $row->name;
    echo $row->body;
}

$query->unbuffered_row();       // object
$query->unbuffered_row('object');   // object
$query->unbuffered_row('array');    // associative array

  

2.单结果标准查询

//对象形式
$query = $this->db->query('SELECT name FROM my_table LIMIT 1'); $row = $query->row(); echo $row->name;
//数组形式

$query = $this->db->query('SELECT name FROM my_table LIMIT 1');
$row = $query->row_array(); echo $row['name'];
//统计select 行数
$query = $this->db->query('SELECT * FROM my_table'); echo $query->num_rows(); /* 并不是所有的数据库驱动器都有原生的方法来获取查询结果的总行数。 当遇到这种情况时,所有的数据会被预读取到内存中,并调用 count() 函数 来取得总行数。 */

  

//统计列数
$query = $this->db->query('SELECT * FROM my_table');

echo $query->num_fields();

  

free_result() 方法

虽说php会自动回收内存,但是当有大量查询时可在执行途中释放内存提高效率。
$query->free_result();  // The $query result object will no longer be available

  

 3.标准插入

尽管查询构造器会尽力保护好你输入的表名和字段名,但值得注意的是, 它并不是被设计来处理任意用户输入的,所以,请不要传未处理的数据给它。
$sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";
$this->db->query($sql);
echo $this->db->affected_rows();
$this->db->insert_string()

这个方法简化了 INSERT 语句的书写,它返回一个正确格式化的 INSERT 语句。 举例:

$data = array('name' => $name, 'email' => $email, 'url' => $url);

$str = $this->db->insert_string('table_name', $data);

  

$this->db->update_string()

这个方法简化了 UPDATE 语句的书写,它返回一个正确格式化的 UPDATE 语句。 举例:

$data = array('name' => $name, 'email' => $email, 'url' => $url);

$where = "author_id = 1 AND status = 'active'";

$str = $this->db->update_string('table_name', $data, $where);

  

  4.查询绑定

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

  5.错误处理

if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`'))
{
    $error = $this->db->error(); // Has keys 'code' and 'message'
}

  

6.查询构造

 

$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();  // Produces: SELECT title, content, date FROM mytable

 

foreach ($query->result() as $row)
{
    echo $row->title;
}

//产生sql语句,可进行检查
$sql = $this->db->get_compiled_select('mytable');
echo $sql;
 

  

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

  

//join操作
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();

  

where 操作


1.
$this->db->where('name', $name); // Produces: WHERE name = 'Joe'
2.
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
3.
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);

  

 

//模糊
$this->db->like('body', 'match');
// WHERE `title` LIKE '%match%' ESCAPE '!' AND  `body` LIKE '%match% ESCAPE '!'

  

//模糊搜索
$this->db->like('title', 'match', 'before');    // Produces: WHERE `title` LIKE '%match' ESCAPE '!'
$this->db->like('title', 'match', 'after'); // Produces: WHERE `title` LIKE 'match%' ESCAPE '!'
$this->db->like('title', 'match', 'both');  // Produces: WHERE `title` LIKE '%match%' ESCAPE '!'

  

//多重模糊查询
$this->db->like('title', 'match'); $this->db->or_like('body', $match);
// WHERE `title` LIKE '%match%' ESCAPE '!' OR  `body` LIKE '%match%' ESCAPE '!'

  

//order_by
$this->db->order_by('title', 'DESC');

  

//limit
$this->db->limit(10, 20);

  

//返回结果行数
echo $this->db->count_all_results('my_table');  // Produces an integer, like 25
$this->db->like('title', 'match');
$this->db->from('my_table');
echo $this->db->count_all_results(); // Produces an integer, like 17

  

多重条件实例条件组必须要配对,确保每个 group_start() 方法都有一个 group_end() 方法与之配对。
$this->db->select('*')->from('my_table')
    ->group_start()
        ->where('a', 'a')
        ->or_group_start()
            ->where('b', 'b')
            ->where('c', 'c')
        ->group_end()
    ->group_end()
    ->where('d', 'd')
->get();

// Generates:
// SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'

  

  

 

转载于:https://www.cnblogs.com/yang95/articles/5832253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值