代码改变世界

ci框架怎么读写操作mysql数据库

2011-01-02 09:42  一片温柔  阅读(199)  评论(0)    收藏  举报

 ci框架中,怎么和数据库交互,下面是我写的简单示例函数:

 

function OprData(){
        $this->load->database();

        //返回多条记录
        $query = $this->db->query('select * from t_appinfo');
        if($query->num_rows()>0){
            foreach($query->result() as $row){
                echo "<br/>";
                echo $row->nID;
                echo $row->sAppName;
                echo $row->tTime;
            }
        }


        //插入数据

        $name='test';
        $sql = "insert into t_appinfo(sAppName,tTime) values(".$this->db->escape($name).",now())";
        $this->db->query($sql);
        echo "<br/>row:";
        echo $this->db->affected_rows();
        //返回单条记录
        $sql = "select * from t_appinfo order by ttime desc limit 1";
        $query = $this->db->query($sql);
        $row = $query->row();
        echo"<br/> time:".$row->tTime;
         //更新数据
        $sql = "update t_appinfo set sAppName='".$row->sAppName."11' where nid=".$row->nID;
        $query = $this->db->query($sql);
        echo "<br/>update row:";
        echo $this->db->affected_rows()."//".$query;
        //删除数据
        $sql="delete from t_appinfo limit 1 ";
         $query = $this->db->query($sql);
        echo "<br/>delete row:";
        echo $this->db->affected_rows()."//".$query;
    }