CodeIgniter Forums

Full Version: DISTINCT Does not work as a keyword in codeigniter....
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

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

El Forum

[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??

El Forum

[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.

El Forum

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

El Forum

[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;
}

El Forum

[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

El Forum

[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();
}

El Forum

[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;
}