- 25、显示最后一条查询的sql语句:主要用于在连贯操作时,检测拼接的sql语句是否正确
- echo $this->db->last_query();//如:select * from pt_users where uid>10 order by datetime desc limit 0,10
- 26、CI_DB_pdo_driver PDO数据库驱动类
- $this->db->affected_rows();//影响记录数(事务中常用),区分:$this->db->get("order_master")->num_rows();//获取到的结果集行数
- $this->db->count_all("order_master");//对于某个表不带条件的查询
- $this->db->count_all_results();//快捷操作类方法,适用于带条件的查询
- $this->db->insert_id();//新插入记录的id(常用于插入是否成功的判断)
- $this->db->trans_enabled = true;//开启事务(默认是false,所以在使用事务前必须将其赋值为true)
- $this->db->trans_begin();//开始事务
- $this->db->trans_rollback();//事务回滚
- $this->db->trans_commit();//提交事务
- $this->db->trans_status();//事务状态 true 或 false CI_DB_driver驱动类中的方法
- 27、CI_DB_mysql_driver mysql数据库驱动类
- $this->db->affected_rows();//影响记录数(事务中常用),区分:$this->db->get("order_master")->num_rows();//获取到的结果集行数
- $this->db->count_all("order_master");//对于某个表不带条件的查询
- $this->db->count_all_results();//快捷操作类方法,适用于带条件的查询
- $this->db->insert_id();//新插入记录的id(常用于插入是否成功的判断)
- $this->db->trans_enabled = true;//开启事务(默认是false,所以在使用事务前必须将其赋值为true)
- $this->db->trans_begin();//开始事务
- $this->db->trans_rollback();//事务回滚
- $this->db->trans_commit();//提交事务
- $this->db->trans_status();//事务状态 true 或 false CI_DB_driver驱动类中的方法
- 28、CI_DB_mysqli_driver mysqli数据库驱动类
- $this->db->affected_rows();//影响记录数(事务中常用)<span id="transmark"></span>,区分:$this->db->get("order_master")->num_rows();//获取到的结果集行数
- $this->db->count_all("order_master");//对于某个表不带条件的查询
- $this->db->count_all_results();//快捷操作类方法,适用于带条件的查询
- $this->db->insert_id();//新插入记录的id(常用于插入是否成功的判断)<span id="transmark"></span>
- $this->db->trans_enabled = true;//开启事务(默认是false,所以在使用事务前必须将其赋值为true)
- $this->db->trans_begin();//开始事务
- $this->db->trans_rollback();//事务回滚
- $this->db->trans_commit();//提交事务
- $this->db->trans_status();//事务状态 true 或 false CI_DB_driver驱动类中的方法
- 29、model模型类中引用其它model模型类(如:category_model)和数据库(如:product)
- public function __construct() {
- parent::__construct();
- $this->product_db = $this->load->database('product', true);//通过model基类中的__get()方法选择性的引入CI超级对象中已加载类库,如:"load"
- $this->load->model('category_model');
- }
- 30、控制器中引用其它模型类(如:category_model)和数据库(如:product)
- public function __construct() {
- parent::__construct();
- $this->product_db = $this->load->database('product', true);
- $this->load->model('category_model');
- }
- 31、helper函数中引用CI超级对象的方法
- function get_order_status_by_order($order_status){
- $CI =& get_instance();//获取CI超级对象
- $CI->load->Model('order_model');//通过CI超级对象可以载入任何模型
- }
- 32、缓存驱动的加载方式
- $this->load->driver('cache', array('adapter' => 'memcached'));//加载缓存驱动或缓存适配器,当前为memcached缓存;注意:CI框架只支持memcached,不支持memcache,windows操作系统下只有memcache扩展
- $this->load->driver('cache', array('adapter' => 'file'));//加载缓存驱动或缓存适配器,当前为file缓存
- $this->load->driver('cache', array('adapter' => 'redis'));//加载缓存驱动或缓存适配器,当前为redis缓存
- $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));//优先选择apc缓存,file文件缓存作为替代方案;如果服务器不支持apc缓存时,选择file文件缓存
- 33、静态html模板文件中如何动态加载区域块内容
- //index.html文件
- <div include="/index.php/pub/common_nav" rel="include"></div>
- //jquery代码
- $(document).ready(function () {
- $('[rel=\'include\']').each(function (e) {
- var t = $(this),
- url = t.attr('include') + location.search;
- url && $.get(url, function (data) {//url:'/index.php/pub/common_nav'
- t.html(data);
- })
- })
- })
- 34、拼接insert sql语句
- /**
- *一维关联数组,拼接sql语句
- *$data['username']="admin";
- *$data['password']="12345";
- *$data['sex']="";
- */
- function add_user( $data ) {
- if ( empty($data) || !is_array($data) ) {
- return false;
- }
- foreach ($data as $key => $value) {
- if ( $value === '') {
- unset($data[$key]);//删除数组中值为空的元素
- }
- }
- $cols = array_keys($data);//获取数组所有的键名
- $values = array_values($data);//获取数组所有的值
- $cols_str = implode(",", $cols);//将数组所有的键名拼接成一个字符串
- $values_str = "'".implode("','", $values)."'";//将数组所有的键值放到单引号中
- //拼接sql:INSERT INTO user (username,password) VALUES ('admin','12345');
- $sql = "INSERT INTO user ({$cols_str}) VALUES ({$values_str})";//拼接sql
- $this->db->query($sql);
- }
- 35、拼接insert sql语句
- /**
- *一维关联数组,拼接sql语句
- *$data['username']="admin";
- *$data['password']="12345";
- *$data['sex']="";
- */
- function add_user( $data ) {
- if ( empty($data) || !is_array($data) ) {
- return false;
- }
- foreach ($data as $key => $value) {
- if ( $value === '') {
- unset($data[$key]);//删除数组中值为空的元素
- }
- }
- $cols = array_keys($data);//获取数组所有的键名
- $values = array_values($data);//获取数组所有的值
- foreach($values as $k=>$val){
- $values[$k]="'".$val."'";//将所有的键值放到单引号中
- }
- $cols_str = implode(",", $cols);//将数组所有的键名拼接成一个字符串
- $values_str = implode(",", $values);//将数组所有的键值拼接成一个字符串
- //拼接sql:INSERT INTO user (username,password) VALUES ('admin','12345');
- $sql = "INSERT INTO user ({$cols_str}) VALUES ({$values_str})";//拼接sql
- $this->db->query($sql);
- }
- 36、拼接update sql语句
- /**
- * 编辑用户信息
- * $userid=1;
- * $data['username']='admin';
- * $data['password']='123';
- */
- function edit_user(userid, $data) {
- if ( empty($data) || !is_array($data) ) {
- return;
- }
- foreach ($data as $key => $value) {
- $str .= isset($str)?", {$key} = '{$value}'":"{$key} = '{$value}'";
- }
- //拼接sql:UPDATE user SET username='admin',password='123' WHERE addr_id = '1';
- $sql = "UPDATE user SET {$str} WHERE addr_id = '{$addr_id}'";
- $this->db->query($sql);
- }
- 37、数据库快捷操作类常用方法
- /**
- * 查询订单
- *
- * @param $query
- */
- public function get_order_list($query, $offset = 0, $limit = 20) {
- if (is_array($query) && !empty($query)) {
- foreach ($query as $key => $val) {
- if (is_array($val)) {
- $this->order_db->where_in($key, $val);
- } else {
- $this->order_db->where($key, $val);
- }
- }
- }
- $this->order_db->order_by('updatetime', 'desc');
- $this->order_db->order_by('id', 'desc');
- if (!$limit) {
- $query = $this->order_db->get('order');
- } else {
- $query = $this->order_db->get('order', $limit, $offset);
- }
- if ($query->num_rows() > 0) {
- return $query->result_array();
- }
- return array();
- }
- 38、拼接select sql语句
- function get_user_list($cols=array("username","password")) {
- $col=implode(",",$cols);//查询的列属性
- $sql = "SELECT $col FROM user ORDER BY addr_id DESC";
- $this->db->query($sql)->result_array();
- }
- 39、CI框架中cookie的三种使用方式
- //第一种设置cookie的方式:采用php原生态的方法设置的cookie的值
- setcookie("user_id",$user_info['user_id'],86500);
- setcookie("username",$user_info['username'],86500);
- setcookie("password",$user_info['password'],86500);
- //echo $_COOKIE['username'];
- //第二种设置cookie的方式:通过CI框架的input类库设置cookie的值
- $this->input->set_cookie("username",$user_info['username'],60);
- $this->input->set_cookie("password",$user_info['password'],60);
- $this->input->set_cookie("user_id",$user_info['user_id'],60);
- //echo $this->input->cookie("password");//适用于控制器
- //echo $this->input->cookie("username");//适用于控制器
- //echo $_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
- //echo $_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值
- //第三种设置cookie的方式:通过CI框架的cookie_helper.php辅助函数库设置cookie的值
- set_cookie("username",$user_info['username'],60);
- set_cookie("password",$user_info['password'],60);
- set_cookie("user_id",$user_info['user_id'],60);
- //echo get_cookie("username");
- 40、array_merge()合并数组函数的使用
- <?php
- header("content-type:text/html;charset='utf-8'");
- $arr1=array(
- "13012"=>array(
- "brand_id"=>2,
- "category_id"=>3
- )
- );
- $arr2=array(
- "13012"=>array(
- "goods_id"=>3576,
- "goods_name"=>"sanyang"
- )
- );
- /**
- *echo "<pre>";print_r(array_merge($arr1,$arr2));
- *结果:
- *Array
- * (
- * [0] => Array //索引重置为数字索引
- * (
- * [brand_id] => 2
- * [category_id] => 3
- * )
- * [1] => Array
- * (
- * [goods_id] => 3576
- * [goods_name] => sanyang
- * )
- * )
- */
- /**
- *echo "<pre>";print_r(array_merge($arr1['13012'],$arr2['13012']));
- *结果:
- * Array
- * (
- * [brand_id] => 2
- * [category_id] => 3
- * [goods_id] => 3576
- * [goods_name] => sanyang
- * )
- */
- ?>
- 41.json格式数据:
- public function json(){
- $data[0]['goods_id']=3567;
- $data[0]['goods_name']="sanyang";
- $data[1]['goods_id']=3567;
- $data[1]['goods_name']="sanyang";
- echo json_encode($data);exit;
- /**
- * 结果:
- * [
- * {
- * "goods_id": 3567,
- * "goods_name": "sanyang"
- * },
- * {
- * "goods_id": 3567,
- * "goods_name": "sanyang"
- * }
- *]
- */
- }
- 42.联合查询 left join
- //controller控制器
- $query = array(
- 'product_id' => $product_id,
- 'activity.status' => array(1, 2, 0),
- 'activity.is_del' => 0
- );
- $query['activity.activity_id<>'] = $activity_id;
- $goods_list = $this->activity_model->get_activity_good_mapping($query, 0, 0);
- //model模型
- public function get_activity_good_mapping($query,$offset = 0, $limit = 0,$get=''){
- $this->db = $this->activity_db;
- if (is_array($query) && !empty($query)) {
- foreach ($query as $key => $val) {
- if (is_array($val)) {
- $this->db->where_in($key, $val);
- } else {
- $this->db->where($key, $val);
- }
- }
- }
- $this->db->from('activity_goods');
- $this->db->join('activity', 'activity_goods.activity_id = activity.activity_id','left');
- if (!$limit) {
- } else {
- $query = $this->db->limit($limit, $offset);
- }
- $query = $this->db->get();
- if ($query->num_rows() > 0) {
- return $query->result_array();
- }
- return array();
- }
- 43.ci框架如何记录sql日志?
- (1)配置日志目录: 在"application/config/config.php" 文件中配置日志目录
- $config['log_path']="/opt/www/logs/";
- (2)在 "system/database/DB_driver.php" 文件的query()方法末尾添加如下语句:
- log_message( 'db','【sql语句:'.$this->last_query().'】');//这样所有执行过的sql语句都会按日期时间格式记录到 "/opt/www/logs/" 目录下
- return $RES;
- //44、根据条件获取记录数(效率最高版)
- public function get_record_count_by_condition($tablename,$condition,$likecondition,$cols){
- if(!empty($cols)){
- $this->db->select("count(*) as num");
- }else{
- $this->db->select("count(*) as num");
- }
- if (is_array($condition) && !empty($condition)) {
- foreach ($condition as $key => $val) {
- if (is_array($val)) {
- $this->db->where_in($key, $val);
- } else {
- $this->db->where($key, $val);
- }
- }
- }
- if (is_array($likecondition) && !empty($likecondition)) {
- foreach ($likecondition as $key => $val) {
- $this->db->like($key, $val);
- }
- }
- $num_info=$this-><span id="transmark"></span>db->get($tablename)->row_array();
- if($num_info['num'] > 0){
- return $num_info['num'];
- }else{
- return 0;
- }
- }
- //45、去除$aid_arr_temp数组中元素
- foreach($content_list_temp_recommend as $k=>$v){
- $kk=array_search($v['aid'], $aid_arr_temp);//$v['aid']必定是$aid_arr_temp数组内元素之一的情况
- $msg.=$aid_arr_temp[$kk].",";
- if($kk !== false){//只要不是false就是找到了
- unset($aid_arr_temp[$kk]);//删除后,索引键保持不变
- }
- }
- $aid_arr= array_values($aid_arr_temp);//经过array_values()函数处理过后,索引键重新分配
- //46、处理提交数据为0的情况 下拉菜单:""-全部 0-未审核 1-已审核
- //$state = $this->input->post('state', true) ? $this->input->post('state', true):"";//值始终为空
- $state = is_numeric($this->input->post('state', true)) ? $this->input->post('state', true):"";//0时值为0,空时值为空
- if(is_numeric($state)){
- $condition['state']=$state;//字段值为0
- }else{
- $state="";
- }
- $data['state']= (string)$state
- $this->load->view('content_list', $data);
- <td>
- <label>状态:</label>
- </td>
- <td>
- <select name="state" class="combox">
- <option value="" <?php if( "" ===$state)echo 'selected'?>>全部</option>
- <option value="0" <?php if("0" === $state)echo 'selected'?>>未审核</option>
- <option value="1" <?php if("1" === $state)echo 'selected'?>>已审核</option>
- </select>
- </td>