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

CC BY-SA 4.0
2018-08-27T19:55:55.330Z
Forgive my ignorance, but how would you then return the $rows array to your twig template?
Jim Pannell
CC BY-SA 4.0
2018-08-27T20:09:20.383Z
@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
Robin Schambach
CC BY-SA 4.0
2018-08-27T20:27:16.850Z
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).
Jim Pannell
CC BY-SA 4.0
2018-08-27T20:29:05.410Z
Services always only return it variables and controllers proceed those values and pass them to Twig/JS/whatever
Robin Schambach
CC BY-SA 4.0
2018-08-27T20:37:41.603Z
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.
Jim Pannell
CC BY-SA 4.0
2018-08-27T21:56:21.167Z
Usually the source code is the best example you can find. You could join us in slack as well
Robin Schambach
CC BY-SA 3.0
2018-04-16T18:26:20.460Z
2018-04-16T18:26:20.460Z
Robin Schambach
<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>

<plugin-development><craft3><database><query>
Craft 3: Get Data from a custom Table via createCommand()
# 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  for it 
$customer = Customer::find() 
    ->where(['id' => 123]) 
    ->one();

Or a query 
$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**: @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  
> **username_2**: 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).  
> **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  

---

