codeigniter 数据库常用操作

$this->db->select('id,name');
                          $this->db->from('category');
                          //注意sql语句中如果要加括号好像不行,只能自己写条件
                          //$this->db->where("is_show",'1');
                         // $this->db->or_where("id","3)");
                          
                         // $this->db->where("sort !=",'a');//注意要有空格,不然就让第3个参数为false(escape),按自己平常的格式
                        //  $this->db->like('name ','关于');
                           //$this->db->escape();//这个函数用来过滤参数
                          
                          $condition="(is_show = '1' or id = '3')";
                         
                          $this->db->where($condition);
                          $query= $this->db->get()->result_array();
          
                          
                          //直接写sql
                          $sql = "SELECT * FROM category WHERE is_show = ? "; 
                          $query= $this->db->query($sql, array(1));
                          
                 
                 
          
                          echo $query->result_id;
                          var_dump($query);
                          return $query->result_array();
                          
                          //技巧:
                          //
                          //表数据//返回一个包含当前连接数据库中所有表名称的数组。例如:
                          $this->db->list_tables();
                          //有时,在对某个表执行操作之前,使用该函数判断指定表是否存在很有用。返回一个布尔值
                          $this->db->table_exists();
                          
                          //查询辅助函数
                          //这个ID号是执行数据插入时的ID。
                          //$this->db->insert_id()
                          //
                          //当执行写入操作(insert,update等)的查询后,显示被影响的行数。
                          $this->db->affected_rows();
                          /* 
                           * 当使用 UPDATE 查询,MySQL 不会将原值和新值一样的列更新。这样使得 mysql_affected_rows() 函数返回值
                         不一定就是查询条件所符合的记录数,只有真正被修改的记录数才会被返回。 
                             REPLACE 语句首先删除具有相同主键的记录,然后插入一个新记录。本函数返回的是被删除的记录数加上被插入的记录数。
                           */
                          
                          //计算出指定表的总行数并返回。在第一个参数中写入被提交的表名
                          $this->db->count_all('my_table');
                          
                         
                          /*
                           * 这个函数简化了写入数据库的insert函数。它返回一个标准的SQL insert字符串。例如:
                            $data = array('name' => $name, 'email' => $email, 'url' => $url);

                            $str = $this->db->insert_string('table_name', $data);
                            The first parameter is the table name, the second is an associative array with the data to be inserted. The above example produces:


                            第一个参数是表名,第二个是被插入数据的联合数组,上面的例子生成的效果为:
                            INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')
                           */
                           $this->db->insert_string();
                           
                           
                           
                           /*
                            * 这个函数简化了写入数据库的update函数。它返回一个标准的SQL 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);
                            The first parameter is the table name, the second is an associative array with the data to be updated, and the third parameter 
                            is the "where" clause. The above example produces:


                            第一个参数是表名,第二个是被更新数据的关联数组,第三个参数是“where”参数。上面的例子生成的效果为:
                            UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'
                            */
                            $this->db->update_string();
                          
                            
                            //生成查询记录
                            //result()
                            //result_array()
                            //row()
                            //row_array()
                            //
                            //结果集辅助函数
                            //该函数将会返回当前请求的行数。在本例子中, $query 表示当前 SQL 所产生的请求结果对象:
                            //$query->num_rows()
                            //$query->num_fields()//该函数将会返回当前请求的行数。在本例子中, $query 表示当前 SQL 所产生的请求结果对象:
                            //$query->free_result()
                            //
                          //通常情况下, 你会需要提供一个 connection ID 或是一个 result ID, connection ID 可以这样来获得:
                          $this->db->conn_id;
                           
                          //结果集id
                          $query = $this->db->query("SOME QUERY");
                          $query->result_id;
                          
                          //表格字段列表
                          $arr=$this->db->list_fields('table_name');
                          //数据集字段列表
                          $query = $this->db->query('SELECT * FROM some_table'); 
                          $arr=$query->list_fields();
                          //字段是否存在
                          $this->db->field_exists('field_name');
                          //返回一个包含字段名称信息的数组
                          $fields = $this->db->field_data('table_name');
                          foreach ($fields as $field)
                            {
                               echo $field->name,"<br/>";
                               echo $field->type,"<br/>";
                               echo $field->max_length,"<br/>";
                               echo $field->primary_key,"<br/>";
                            }

 

posted on 2011-12-28 14:04  天空尚兰  阅读(907)  评论(0编辑  收藏  举报

导航