——————–写在2018-01-10,这又是一个比较沉重的话题,本是为了解脱另一个问题,有踏入新的泥潭—————–
首先,先掌握使用方式在去了解为什么会有这些个使用方法!!在实际的工作生活中,一定是你有了解决问题的能力体现了自己的价值,然后才会有时间去丰富自己羽翼,甚至再去夯实基础的,不可能任何东西一上来就能让你明白原理,拿出大把时间去学习了之后再去运用的,除了上学时候。
写在前面,难点<1> 预处理 <2> 转义–即关于sql注入问题
<1> 常规查询——$this->db->query(‘your sql statement’);
<2> 查询绑定—–最重要的好处是,所有的值都会被自动转义,生成安全的查询语句。如代码是这样的
$sql = "select * from my_table where filed1 = ? and field2 = ?"
;
$query = $this->query($sql,array(value1,value2));
很明显,查询语句中的问号会被替代,替代的过程中会被自动转义。
<3>
this−>db−>get(′mytable′);用来构建select语句,可以查出一个表中的所有数据,或者limit取数,如
this->db->get(‘my_table’, 10 , 20 );
<3>
this−>db−>from();用法同上,但不能作为处理语句来用,只能做组合语句来用.用法:
this->db->select(‘field’);
this−>db−>from(′mytable′);注意,这时候还不行需要
query =
this−>db−>get();看出与上面写法的不同了吗???<4>
this->db->select(‘field1’ , ‘field2’, ‘field3’); 设置查询字段
<5>
this−>db−>selectmax();/
this->db->select_min(); 获取字段的max/min值,还可以为字段设置别名,
this−>db−>selectmax(′field′,′alias′);alias即为别名.
this->db->select_avg();/
this−>db−>selectsum();<6>
this->db->join();放弃使用这种方法
<7>
this−>db−>where();提供了四种方式,以供查询。<7.1>简单的key/value形式。使用方式:
this->db->where(‘name’,’value’);多个条件时,多次调用,不太方便
<7.2> 自定义的key/value形式。你可以在第一个参数中自定义一些比较字符,如
this−>db−>where(′id<′,
id);
this−>db−>where(′name!=′,
name);
<7.3>关联数组方式,方便多个条件时使用。
this−>db−>where(array(‘key′=>′value′,′key2′=>′value2′));<7.4>自定义字符串。
where = “name = ‘zhuxinlei’ and male = 1 and age = 28 or age = 27”;
this−>db−>where(
where);
<8>
this−>db−>orwhere();这个方法需要和where方法联合使用
this->db->where(‘name’,’zhuxinlei’);
this−>db−>orwhere(‘age′,28);//produce:wherename=zhuxinleiorage=28<9>
this->db->where_in();用于生成where in语句,
id=array(1,2,3);
thid->db->where_in('id',
id);<10>
this->db->where_not_in();
<11>关于like的使用,放弃,我现在基本不适用like
<12>
this−>db−>groupby();<13>
this->db->order_by();可以对多个字段排序
this−>db−>orderby(‘id′,′desc′);
this->db->order_by(‘sort’,’asc’);
<14>
this−>db−>limit();<15>
this->db->insert();简单插入
data=array(‘id′=>”,′name′=>′zhuxinlei′,′age′=>28);
this->db->insert('my_table',
data);<16>
this->db->insert_batch();批量插入
data=array(array(‘id′=>”,′name′=>′zhuxinlei′,′age′=>28),array(‘id′=>”,′name′=>′houmeina′,′age′=>28));
this->db->insert_batch('my_table',
data);<17>更新数据
this->db>replace('my_table',
data);如果新插入的数据中有字段是主键,如果主键值已存在则替换,如果没有则插入<18>
this->db->set();
—————-够了够了,先说这些吧,下面开始真正的学习之路———-
<1>$this->load->database();
我们知道,在ci中是通过这种方式加载数据库对象。回头就可以使用$this->db->func();的方式调用里面的各种方法,那database()方法中经历了what??
ci框架中数据库对象的加载过程及使用方法
最新推荐文章于 2023-07-21 16:50:41 发布