zoukankan      html  css  js  c++  java
  • CI入门

    CI入门

    一、【查】按条件获取一条记录

    获取数据有返回数组形式或对象形式,row_array()、result_array()是以数组形式返回;row()、result()是以对象形式返回。同样的,更新、删除、新增数据,也是可以传数组或对象形式进行操作的。

    方式一

    /**
     * 按sku获取一条数据
     * @param $sku
     * @return mixed
     */
    public function getOneRowBySku($sku)
    {
        $where = ['sku' => $sku, 'is_translated' => 1];
        return $this->db->where($where)->get($this->tableName)->row_array();
    }
    
    

    方式二

    //查物流商详情
    public function getRowArray($code)
    {
        $sql="select id,name,code from ueb_cargo_company where code= '$code'";
        $result = $this->db->query($sql);
        return $result->row_array();
    }
    

    二、【增】插入数据

    //方式一
    $sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";
    $this->db->query($sql);
    if($this->db->affected_rows()){
        echo '插入成功';
    }else{
        echo '插入失败';
    }
    
    
    //方式二
    $data = array(
        'title' => 'My title',
        'name' => 'My Name',
        'date' => 'My date'
    );
    
    $this->db->insert($this->tableName, $data);
    
    //批量插入
    $data = array(
        array(
            'title' => 'My title',
            'name' => 'My Name',
            'date' => 'My date'
        ),
        array(
            'title' => 'Another title',
            'name' => 'Another Name',
            'date' => 'Another date'
        )
    );
    
    $this->db->insert_batch('mytable', $data);
    

    三、【改】更新数据

    //添加更新条件
    $this->db->where('declaration_cn_name', $declaration_cn_name);
    //更新
    $this->db->update($this->tableName, $updateData);
    
    
    // 批量更新
    $data = [
        [
            'title' => 'My title' ,
            'name' => 'My Name 2' ,
            'date' => 'My date 2'
        ],
        [
            'title' => 'Another title' ,
            'name' => 'Another Name 2' ,
            'date' => 'Another date 2'
        ]
    ];
    //按$index字段条件对$data数据集中的数据进行批量更新
    $this->db->update_batch($this->tableName, $data, $index='title');
    //上面,第一个参数为要更新的表名,第二个参数为要更新的数据,是个二维数组,第三个参数是 WHERE 语句的键。
    

    四、【删】删除数据

    //删除一条数据
    $this->db->delete('mytable', array('id' => $id));
    
    //清空表数据
    $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable
    
    

    五、统计满足条件的数据总数

    $where = ['sku' => $sku, 'is_translated' => 1];
    return $this->db->where($where)->get($this->tableName)->num_rows();
    

    六、引入library库文件

    //调用有道翻译接口
    $this->load->library('youdao_translate_api');
    $content = $this->youdao_translate_api->translate($content);
    

    总结

    主要还是看官方文档,官方文档写的很清楚,总结算是提炼自己常用的东西,方便下次快速查到,提高效率

    参考资料

  • 相关阅读:
    LeetCode -- Rectangle Area
    【转】VS常用快捷键
    C++中include<> 与 include" " 的区别
    浅析_tmain() 与 main() 函数的区别
    VS2013下配置OpenCV 3.0.0 &&& VS2013下配置Opencv2.4.9
    LeetCode -- Valid Parenthese
    杂想 · 警醒
    LeetCode -- Length of Last Word
    LeetCode -- Valid Sudoku
    LeetCode -- Binary Tree Level Order Traversal II
  • 原文地址:https://www.cnblogs.com/renzhicai/p/8822575.html
Copyright © 2011-2022 走看看