CodeIgniter Forums
Passing Variables to SQL Query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Passing Variables to SQL Query (/showthread.php?tid=61683)



Passing Variables to SQL Query - Wolrab - 05-05-2015

Hello,

I am trying to pass a session variable = userid to a model which runs a sql query then passes the query back to the controller which passes the data to thew view.

Apologies for the newbie question, if you could point me in the right direction or provide a link to some tutorials that would be much appreciated Smile

function functionname($data){
$query = $this->db->query("SELECT * FROM table WHERE column = $data");

return $query->result();

}


RE: Passing Variables to SQL Query - casa - 05-07-2015

about your function, your query is not protected
PHP Code:
$query $this->db->get_where("table_name", array('column' => $data));
// or 
$sql "SELECT * FROM table_name where column =  ?" ;
$query $this->db->query($sql, array($data)) ; 
About session variable,(documentation about CI is very good)
PHP Code:
// create a session variable
$this->session->set_userdata('name''value') ;
// call a session variable
$this->session->userdata('name') ; // return 'value' 
About use function
PHP Code:
// some codes ...
 //...
  
$this->Model_name->functionname($this->session->userdata('name')) ;