<p>I'd suggest reading through the Yii <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder" rel="nofollow">QueryBuilder docs</a>, which is what the code you've written is using.</p>

<pre><code>-&gt;from('{{ assetfiles }}')
</code></pre>

<p>should be:</p>

<pre><code>-&gt;from('assetfiles')
</code></pre>

<p>And if you want multiple rows returns as a a result, you need to add a:</p>

<pre><code>-&gt;queryAll();
</code></pre>

<p>to the end of the statement.</p>

<p>From there, you can do a <code>foreach</code> loop to access the results of your query:</p>

<pre><code>foreach ($query as $row)
{
    $id = $result['id'];
}
</code></pre>

CC BY-SA 3.0
2016-09-21T03:09:13.893Z
Thank you @Brad, after I posted it started looking in the app folder to see how the code was written and then used it as an example. But this is going to be helping in future when I forget where I wrote my code.
Subrato M
CC BY-SA 3.0
2016-09-21T03:05:15.393Z
2016-09-21T03:05:15.393Z
Brad Bell
<p>So I am using a plugin to import data and it isn't assigning the values (asset is inside of Matrix) so I am going to modify the plugin and add code to lookup values that I need to assign to <code>MatrixBlockModel</code> and then re-save the item. </p>

<p>I looked up how to create a command and based on that I wrote 3 lines of code but I have no idea on how to iterate through the results. Here is the sample code:</p>

<pre><code>$query=craft()-&gt;db-&gt;createCommand()
  -&gt;select('id, folderId, filename, kind')
  -&gt;from('{{ assetfiles }}')
  -&gt;where('filename= :fileName', array(':fileName' =&gt; 'somefilename'));
</code></pre>

<p>How do I iterate through the results and how can I access info from the query?</p>

CC BY-SA 4.0
2023-02-13T18:50:23.240Z
How would this look like in Craft4?
`$command = Craft::$app->getDb()->createCommand()->select(*)->from('table')->where(["id" => $id]);`
Something like this? 

I successfully have the commands for update and insert, but the select method does not exist...
Merc
<plugin-development><queries><createcommand>
Anyone have a createCommand example?
# Anyone have a createCommand example?

(Asked by: username_0 on 2016-09-20)

So I am using a plugin to import data and it isn't assigning the values (asset is inside of Matrix) so I am going to modify the plugin and add code to lookup values that I need to assign to MatrixBlockModel and then re-save the item. 
I looked up how to create a command and based on that I wrote 3 lines of code but I have no idea on how to iterate through the results. Here is the sample code:
$query=craft()->db->createCommand()
  ->select('id, folderId, filename, kind')
  ->from('{{ assetfiles }}')
  ->where('filename= :fileName', array(':fileName' => 'somefilename'));

How do I iterate through the results and how can I access info from the query?

**Tags:** plugin-development, queries, createcommand

> **Comments:**
> **username_1**: How would this look like in Craft4? `$command = Craft::$app->getDb()->createCommand()->select(*)->from('table')->where(["id" => $id]);` Something like this?   I successfully have the commands for update and insert, but the select method does not exist...  

---

## 1 answers

### Answer by username_2 on 2016-09-21. Score: 1 | Accepted answer

I'd suggest reading through the Yii QueryBuilder docs, which is what the code you've written is using.
->from('{{ assetfiles }}')

should be:
->from('assetfiles')

And if you want multiple rows returns as a a result, you need to add a:
->queryAll();

to the end of the statement.
From there, you can do a foreach loop to access the results of your query:
foreach ($query as $row)
{
    $id = $result['id'];
}

> **Comments:**
> **username_0**: Thank you @Brad, after I posted it started looking in the app folder to see how the code was written and then used it as an example. But this is going to be helping in future when I forget where I wrote my code.  

---

