CodeIgniter Sample Code (MVC) Data Access - Select

CodeIgniter Sample Code (MVC) Data Access - Select

Select * from comments

1. Model : data_model.php

class Data_model extends CI_Model

{

/* 스트링으로 바로 집어 넣는 방법

function getall()

{

$q = $this->db->query("SELECT * FROM comments");

if($q->num_rows() >0)

{

foreach($q->result() as $row)

{

$data[] = $row;

}

return $data;

}

}

*/

/* 액티브 레코드 사용

function getAll()

{

$q = $this->db->get('comments');

if($q->num_rows()>0)

{

foreach ($q->result() as $row)

{

$data[] = $row;

}

return $data;

}

}

*/

/*

function getAll()

{

$this->db->select('title, contents');

$q= $this->db->get('comments');

if($q->num_rows()>0)

{

foreach ($q->result() as $row)

{

$data[] = $row;

}

return $data;

}

}

*/

/* 조건 붙어서 Select 하는 방법

function getAll(){

$sql = "SELECT title, author, contents FROM comments WHERE id=? OR author = ?";

$q = $this->db->query($sql, array(4, 'mike whoevers'));

if($q->num_rows()>0)

{

foreach ($q->result() as $row)

{

$data[] = $row;

}

return $data;

}

}

*/

//

function getAll(){

$this->db->select('title, contents');

$this->db->from('comments');

$this->db->where('id', 5);

$q = $this->db->get();

if($q->num_rows()>0)

{

foreach ($q->result() as $row)

{

$data[] = $row;

}

return $data;

}

}

}

?>

from http://yobine.tistory.com/286 by ccl(A) rewrite - 2021-10-27 22:26:55