hey I am new to codeigniter. I am having problems in fetching data from my database here is my getOne function
public function getOne($sku){
$ans = 0;
$query = $this->db->query('SELECT * FROM barcode_sku WHERE sku = "$sku"');
$res = $query->result();
$row = $res[0];
$ans = $row->quantity;
return $ans;
}
the variable $sku will have values like bc_001 or bc_002.... The problem is if I hard code this value i.e bc_001 in my query it fetches the result correctly however when I use the variable $sku in my query it does not work. please help.
Because you're using single quotes to warp your query statement php will not prase the variable inside that query, instead use the double quotations to allow php to interpret the $sku variable.
$query = $this->db->query("SELECT * FROM barcode_sku WHERE sku = \"$sku\"");
Also don't forget to escape $sky variable to avoid SQL Injections.
a better solution is to use codeigniter active record.
http://www.codeigniter.com/userguide2/database/active_record.html