질문
Q #8841
CI 입문자입니다 도와주세요~ㅠㅠㅠ
할랭보
2010-03-05 16:06:05
간단하게 mysql로 db에 연동하는 프로그램을 만들고 있는데요~
결과값이 잘 안나와 이렇게 질문드립니다. 답변 주시면 많은 도움이 될 것 같습니다.
//controller
<?php
class Test_theater extends Controller{
function Test_theater()
{
parent::Controller();
$this->load->database();
}
function index()
{
echo "함수가없을때";
}
function theater_list(){
$this->load->model("table_list");
$query=$this->table_list->get_tbl_list();
$res=$query->result_array();
$this->load->view("list_v",$res);
}
}
?>
//model
<?php
class Table_list extends Model{
function Table_list(){
parent::Model();
$this->load->database();
}
/*function index(){
echo 'model aaa';
} */
function get_tbl_list(){
$sql="select * from theater_m where theater_type='A'";
$res = $this->db->query($sql);
return $res;
}
}
?>
뷰에서 $res가 나오질 않네요 쿼리부분에서 잘못된거같은데 좀 도와주세요
컨트롤러에서만 선언해주시면 됩니다.
그리고 매뉴얼(또 이야기하지만)을 좀더 보셔야 할 것 같습니다.
위의 get_tbl_list()는 mysql_query()까지만 한 상태입니다.
결과를 뽑아오려면 mysql_fetch_array()를 해줘야 결과가 나오겠죠?
ci에서는 $res = $this->db->query($sql); 다음 라인에
$result = $res->result(); 를 추가하셔야 합니다.
그리고 위와 같이 일반 php에서 사용하는 sql문으로 쿼리를 날리는 방식을 사용하면
좋은 툴을 놔두고 사용하지 않는 것과 같습니다.
ci에는 액티브레코드라는 것이 있고 사용하게 되면 sql injection을 자동으로 막아줍니다.
(위 함수는 sql문에 escape처리를 해주셔야 안전합니다)
http://codeigniter-kr.org/user_guide/database/active_record.html
위 함수를 액티브레코드를 사용한 것으로 바꿔보면
$res = $this->db->get_where('theater_m', array('theater_type'=>'A'));
$result = $res->result();
return $result;