Answers.Body: <p>For every table CiviCRM has api, you can use following method to retrieve data by specifying <strong>return</strong> in api params.</p>
  
  <p>1 - <strong>Get</strong> -  Returns one or more results and with N number of columns</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'get', [
    'return' =&gt; ["id", "display_name"],
    'contact_sub_type' =&gt; "Parent",
  ]);
  </code></pre>
  
  <p>2 - <strong>GetValue</strong> - Returns only single result and with single column (make sure to use id in filer or limit =1 else the api will throw error if the api returns more than one results)</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'getvalue', [
    'return' =&gt; "display_name",
    'id' =&gt; 1,
  ]);
  </code></pre>
  
  <p>3 - <strong>GetSingle</strong> - Returns only single result and with N number of columns (make sure to use id in filer or limit =1 else the api will throw error if the api returns more than one results)</p>
  
  <pre><code>$result = civicrm_api3('Contact', 'getsingle', [
    'return' =&gt; ["display_name", "email"],
    'id' =&gt; 1,
  ]);
  </code></pre>
  
  <p>Incase if you want to retrieve from custom table created using extension then you will need to make sure you have </p>
  
  <ol>
  <li>New entity defined - </li>
  <li>DAO for the table</li>
  <li>api with create, delete and get action</li>
  </ol>
  
  <p>You can use <a href="https://docs.civicrm.org/dev/en/latest/extensions/civix/#generate-entity" rel="nofollow noreferrer">Civix</a> to create new entity which will create above 3 points automatically for you.</p>
  
  <p>Then you can use below snippet to build your select field</p>
  
  <pre><code>    $this-&gt;add('select', 
        'name',         //field type 
        ts('Name'), //field label
        CRM_Utils_Array::collect('name', civicrm_api3('Emp', 'get', [
          'return' =&gt; ["id", "name"],
        ])['values']) //list (id, name) 
      );
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T10:32:54.570Z
Answers.Text: I have an id and user_name columns in table. I want to fetch and put as option value i.e. <option value='id'>user_name </option>
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T10:49:14.050Z
Answers.Text: Which table you talking about? Is it a civi table or your own table?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T10:53:34.137Z
Answers.Text: My custom table. Check the above image.
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T10:58:33.923Z
Answers.Text: Updated my answer to include your case
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T11:07:07.267Z
Answers.Text: I create a module using civix. Please refer edited question.
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T11:15:48.323Z
Answers.Text: So do you have Emp api defined?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T11:16:56.000Z
Answers.Text: Yes Sir. DAO BAO and API. I fetch that value Using CiviCRM API 3, .
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T11:23:57.653Z
Answers.Text: Updated the answer, please check!
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T11:28:46.050Z
Answers.Text: Thanks Sir. But how this code is working? I am not understanding.
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T13:30:55.003Z
Answers.Text: You can get idea how this works at https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/Array.php#L613 . So can you please mark this as answered my answer solves your problem?
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T14:24:39.903Z
Answers.Text: I already marked as true answer.
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T15:33:26.273Z
Answers.Text: I think you voted up rather marking it as answered
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-23T07:06:29.887Z
Answers.Text: Both voted as well as marked.
Answers.UserDisplayName: deepak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-22T10:19:20.533Z
Answers.LastActivityDate: 2019-01-22T11:23:38.447Z
Answers.LastEditDate: 2019-01-22T11:23:38.447Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>Currently I am using API for fetch the data.<br>
  I am creating an extension.
  I want to fetch the particular column's data from the table. How will I get the data?</p>
  
  <pre><code>//getting id and name
  public static function getName(){
   $data = ($id,$name);
    $result = civicrm_api3('Emp', 'get', [
        'sequential' =&gt; 1,
        'return' =&gt; ["id", "name"],
    ]);
  
    foreach ($result['values'] as $value) {
        $data[$id] += $value['id'];
        $data[$name] += $value['name'];
  
    }
    return $data;
  } 
  </code></pre>
  
  <blockquote>
    <p>Select option using BuildQuickForm</p>
  </blockquote>
  
  <pre><code>$this-&gt;add('select', 
        'name',         //field type 
        'name', //field label
        $this-&gt;getName() //list (id, name) 
        );
  </code></pre>
  
  <p><a href="https://i.sstatic.net/XkIdQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XkIdQ.png" alt="enter image description here"></a></p>
  
Tags: <extension>
Title: How to fetch perticular column data of table?
ThreadText: # How to fetch perticular column data of table?
  
  (Asked by: username_0 on 2019-01-22)
  
  Currently I am using API for fetch the data.
  I am creating an extension.
  I want to fetch the particular column's data from the table. How will I get the data?
  //getting id and name
  public static function getName(){
   $data = ($id,$name);
    $result = civicrm_api3('Emp', 'get', [
        'sequential' => 1,
        'return' => ["id", "name"],
    ]);
  
    foreach ($result['values'] as $value) {
        $data[$id] += $value['id'];
        $data[$name] += $value['name'];
  
    }
    return $data;
  } 
  
  
  Select option using BuildQuickForm
  
  $this->add('select', 
        'name',         //field type 
        'name', //field label
        $this->getName() //list (id, name) 
        );
  
  **Tags:** extension
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-22. Score: 2
  
  For every table CiviCRM has api, you can use following method to retrieve data by specifying return in api params.
  1 - Get -  Returns one or more results and with N number of columns
  $result = civicrm_api3('Contact', 'get', [
    'return' => ["id", "display_name"],
    'contact_sub_type' => "Parent",
  ]);
  
  2 - GetValue - Returns only single result and with single column (make sure to use id in filer or limit =1 else the api will throw error if the api returns more than one results)
  $result = civicrm_api3('Contact', 'getvalue', [
    'return' => "display_name",
    'id' => 1,
  ]);
  
  3 - GetSingle - Returns only single result and with N number of columns (make sure to use id in filer or limit =1 else the api will throw error if the api returns more than one results)
  $result = civicrm_api3('Contact', 'getsingle', [
    'return' => ["display_name", "email"],
    'id' => 1,
  ]);
  
  Incase if you want to retrieve from custom table created using extension then you will need to make sure you have 
  
  New entity defined - 
  DAO for the table
  api with create, delete and get action
  
  You can use Civix to create new entity which will create above 3 points automatically for you.
  Then you can use below snippet to build your select field
      $this->add('select', 
        'name',         //field type 
        ts('Name'), //field label
        CRM_Utils_Array::collect('name', civicrm_api3('Emp', 'get', [
          'return' => ["id", "name"],
        ])['values']) //list (id, name) 
      );
  
  HTH
  Pradeep
  
  > **Comments:**
  > **username_0**: I have an id and user_name columns in table. I want to fetch and put as option value i.e. <option value='id'>user_name </option>  
  > **username_1**: Which table you talking about? Is it a civi table or your own table?  
  > **username_0**: My custom table. Check the above image.  
  > **username_1**: Updated my answer to include your case  
  > **username_0**: I create a module using civix. Please refer edited question.  
  > **username_1**: So do you have Emp api defined?  
  > **username_0**: Yes Sir. DAO BAO and API. I fetch that value Using CiviCRM API 3, .  
  > **username_1**: Updated the answer, please check!  
  > **username_0**: Thanks Sir. But how this code is working? I am not understanding.  
  > **username_1**: You can get idea how this works at https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/Array.php#L613 . So can you please mark this as answered my answer solves your problem?  
  > **username_0**: I already marked as true answer.  
  > **username_1**: I think you voted up rather marking it as answered  
  > **username_0**: Both voted as well as marked.  
  
  ---
  
  