Thursday, February 3, 2011

Handling Database In CI

To handle database in CodeIgniter by traditional way, 
1. set default setting in application/config/database.php to your database setting
2. set application/config/autoload.php to autoload['libraries'] = array('database');
3.Then do like this below in controller:
    $query = $this->db->query('SELECT * FROM musim');
    foreach($query->result() as $row){
        echo $row->id;     // id in table
        echo $row->name;   // name in table
        echo $row->city;   // city in table
    }
    echo 'Total Rows: '.$query->num_rows();

4. For inserting some data, use
    $query = "INSERT INTO musim VALUES (null,".$musim.")";
    $this->db->query($query);
    echo $this->db->affected_rows();

No comments:

Post a Comment