CodeIgniter Forums
DISTINCT Does not work as a keyword in codeigniter.... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: DISTINCT Does not work as a keyword in codeigniter.... (/thread-49565.html)



DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]RaviDalal[/eluser]
I am writing following codeigntier function using which i want to fetch distinct record for contact_type field..........

function getdetaildatabyid($id)
{
$this->db->select("contact_details_id,contact_id,contact_type,contact_value");
$this->db->from("tbl_contact_details");
$this->db->where("tbl_contact_details.contact_id",$id);
$query = $this->db->get();
$data = $query->result();
echo $this->db->last_query();die;
return $data;
}


In above given code where should i put distinct to fetch only single occurance of contact_type record????

Please Help.......

Thnx in advance

Ravi Dalal


DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]aquary[/eluser]
Put it where you'd normally put in SQL, which is inside the select.


DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]RaviDalal[/eluser]
thnx for your reply

but i have tried it already....and codeigniter shows error if we put DISTINCT(capital/small) in select statement....

can u help me some else??


DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]aquary[/eluser]
try put in a FALSE as a second parameter of select(), If it's still not working, please copy the error you see, including the query.


DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]RaviDalal[/eluser]
can you send me sample query.....where to write FALSE in query???


DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]aquary[/eluser]
simplified version of yours:
Code:
function getdetaildatabyid($id)
{
  $data=$this->db->select(“DISTINC contact_details_id,contact_id,contact_value”, FALSE)
     ->from(“tbl_contact_details”)
     ->where(“tbl_contact_details.contact_id”,$id)
     ->get()
     ->result();
  echo $this->db->last_query();
  return $data;
}



DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]RaviDalal[/eluser]
thnx for ur response but it shows following error:



<br />
<b>Fatal error</b>: Call to a member function result() on a non-object in <b>C:\wamp\www\final_product_ver01\application\models\model_contact.php</b> on line <b>130</b><br />

that means if i put distinct in my select statement....then it does not fetch any record


DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]aquary[/eluser]
What version of CI you are using? I remember in the old version I cannot use result() directly if there is no data.

Try this one and let see what are returned:

Code:
function getdetaildatabyid($id)
{
  $data=$this->db->select(“DISTINC contact_details_id,contact_id,contact_value”, FALSE)
     ->from(“tbl_contact_details”)
     ->where(“tbl_contact_details.contact_id”,$id)
     ->get();
  var_dump($data);
  // Maybe you could use these commented lines instead.
  // if($data->num_rows()>0)
  //    return $data->result();
  // else
  //    return array();
  return $data->result();
}



DISTINCT Does not work as a keyword in codeigniter.... - El Forum - 02-25-2012

[eluser]solid9[/eluser]
not tested, but try this,


function getdetaildatabyid($id)
{
$this->db->select(“contact_details_id,contact_id,contact_value”);
$this->db->distinct('contact_type');
$this->db->from(“tbl_contact_details”);
$this->db->where(“tbl_contact_details.contact_id”,$id);
$query = $this->db->get();
$data = $query->result();
echo $this->db->last_query();die;
return $data;
}

or try this below,
function getdetaildatabyid($id)
{
$this->db->distinct('contact_type');
$this->db->from(“tbl_contact_details”);
$this->db->where(“tbl_contact_details.contact_id”,$id);
$query = $this->db->get();
$data = $query->result();
echo $this->db->last_query();die;
return $data;
}