codeigniter的数据库操作

    —————————————————–
  • 链接数据库
  • ——-
  • $this->load->database();//手动连接数据库
  • //连接多数据库
  • $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'];
  • */
  •  
  •  
  • —————————————————–
  • 插入
  • ——-
  • $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)
  •  
  • —————————————————–
  • 更新
  • ——-
  • $data = array(
  •                 ‘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() //释放资源
  • 查询数据

    方法1:

    $query = $this->db->get('mytable');
    // Produces: SELECT * FROM mytable

    第二和第三个参数允许你设置一个结果集每页纪录数(limit)和结果集的偏移(offset)

    $query = $this->db->get('mytable', 10, 20);
    // Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
    foreach ($query->result() as $row)
    {
        echo $row->title;
    }
    <span style="font-family: Courier New;">方法2</span>
     

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

      

    一个来自国外论坛的模糊查询+联合查询的例子

    http://codeigniter.com/forums/viewthread/123755/#615985

    1. Ask:I’m trying to create a search-function that searches through 3 tables at once. I’m not sure I’m approaching this in the right way:

    $this-&gt;db-&gt;like('name', $search); // users table
    $this-&gt;db-&gt;like('company', $search); // companies table
    $this-&gt;db-&gt;like('company_adress', $search); // companies table
    $this-&gt;db-&gt;like('interests', $search); // interests table        
    $this-&gt;db-&gt;from('users');
    $this-&gt;db-&gt;join('companies', 'companies.user_id = users.id');
    $this-&gt;db-&gt;join('interests', 'interests.user_id = users.id');
    $q=$this-&gt;db-&gt;get(); 

    One user can have multiple companies and multiple interests.

    2. David: would try something like this:

    $this-&gt;db-&gt;like('name', $search); // users table
    $this-&gt;db-&gt;or_like('company', $search); // companies table
    $this-&gt;db-&gt;or_like('company_adress', $search); // companies table
    $this-&gt;db-&gt;or_like('interests', $search); // interests table        
    $this-&gt;db-&gt;from('users u');
    $this-&gt;db-&gt;join('companies c', 'c.user_id = u.id', 'left');
    $this-&gt;db-&gt;join('interests i', 'i.user_id = u.id', 'left');
    $this-&gt;db-&gt;group_by('u.id'); // added a group_by
    $q=$this-&gt;db-&gt;get(); 

    the third parameter ‘left’ in the join argument makes sure that you can search for a user with no interests or companies.

    3.Wow David, this works like a charm! Thanks so much!

    As I understand it now, the “left” statement makes sure the table “users” is the main table in this query, no matter what’s in the other tables.

     

    

    回复

    你的邮件地址不会被公开(Your email address will not be published.) Required fields are marked *

    *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>