Answers.Body: <p>You can use either an <a href="https://www.yiiframework.com/doc/guide/2.0/en/db-active-record" rel="noreferrer">activeRecord</a>  for it </p>
  
  <pre><code>$customer = Customer::find() 
      -&gt;where(['id' =&gt; 123]) 
      -&gt;one();
  </code></pre>
  
  <p>Or a <a href="https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder" rel="noreferrer">query</a> </p>
  
  <pre><code>$rows = (new \craft\db\Query()) 
      -&gt;select(['id', 'email']) 
      -&gt;from('user') 
      -&gt;where(['last_name' =&gt; 'Smith']) 
      -&gt;limit(10)
       -&gt;all();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-27T19:55:55.330Z
Answers.Text: Forgive my ignorance, but how would you then return the $rows array to your twig template?
Answers.UserDisplayName: Jim Pannell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-27T20:09:20.383Z
Answers.Text: @JimPannell  It depends on what you are doing. If you call this function via `Variable` you can just return it. If you call it via controller request you can render it `return $this->renderTemplate('path/to/template' ['data' => $rows])` if it is an ajax request you need to encode it to json  within your Controller `return $this->asJson(['data' => $rows]);` It's hard to tell without further information
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-27T20:27:16.850Z
Answers.Text: Thanks @Robin-Schambach. It's actually in a service file (I understood this is where most of my DB calls should be in Craft 3).
Answers.UserDisplayName: Jim Pannell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-27T20:29:05.410Z
Answers.Text: Services always only return it variables and controllers proceed those values and pass them to Twig/JS/whatever
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-27T20:37:41.603Z
Answers.Text: I'm new to Craft dev work and have been finding the lack of Craft 3 examples and documentation really hard going. Your comments help a lot - thanks.
Answers.UserDisplayName: Jim Pannell
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-27T21:56:21.167Z
Answers.Text: Usually the source code is the best example you can find. You could join us in slack as well
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-16T18:26:20.460Z
Answers.LastActivityDate: 2018-04-16T18:26:20.460Z
Answers.UserDisplayName: Robin Schambach
Body: <p>In Craft 2 you could creat a query with:</p>
  
  <p><code>craft()-&gt;db-&gt;createCommand()-&gt;select()</code> .... etc.</p>
  
  <p>But in Craft 3 there is no <code>select()</code> Method in <code>Craft::$app-&gt;db-&gt;createCommand()</code>.</p>
  
  <p>Am I overlooking something?</p>
  
Tags: <plugin-development><craft3><database><query>
Title: Craft 3: Get Data from a custom Table via createCommand()
ThreadText: # Craft 3: Get Data from a custom Table via createCommand()
  
  (Asked by: username_0 on 2018-04-16)
  
  In Craft 2 you could creat a query with:
  
  `craft()->db->createCommand()->select()` .... etc.
  
  But in Craft 3 there is no `select()` Method in `Craft::$app->db->createCommand()`.
  
  Am I overlooking something?
  
  **Tags:** plugin-development, craft3, database, query
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-16. Score: 9 | Accepted answer
  
  You can use either an [activeRecord](https://www.yiiframework.com/doc/guide/2.0/en/db-active-record) for it
  
  ```
  $customer = Customer::find() 
      ->where(['id' => 123]) 
      ->one();
  
  ```
  
  Or a [query](https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder)
  
  ```
  $rows = (new \craft\db\Query()) 
      ->select(['id', 'email']) 
      ->from('user') 
      ->where(['last_name' => 'Smith']) 
      ->limit(10)
       ->all();
  
  ```
  
  > **Comments:**
  > **username_2**: Forgive my ignorance, but how would you then return the $rows array to your twig template?  
  > **username_1**: @username_2  It depends on what you are doing. If you call this function via `Variable` you can just return it. If you call it via controller request you can render it `return $this->renderTemplate('path/to/template' ['data' => $rows])` if it is an ajax request you need to encode it to json  within your Controller `return $this->asJson(['data' => $rows]);` It's hard to tell without further information  
  > **username_2**: Thanks @username_1-username_1. It's actually in a service file (I understood this is where most of my DB calls should be in Craft 3).  
  > **username_1**: Services always only return it variables and controllers proceed those values and pass them to Twig/JS/whatever  
  > **username_2**: I'm new to Craft dev work and have been finding the lack of Craft 3 examples and documentation really hard going. Your comments help a lot - thanks.  
  > **username_1**: Usually the source code is the best example you can find. You could join us in slack as well  
  
  ---
  
  