Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-21T03:09:13.893Z
Answers.Text: 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.
Answers.UserDisplayName: Subrato M
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-21T03:05:15.393Z
Answers.LastActivityDate: 2016-09-21T03:05:15.393Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-02-13T18:50:23.240Z
Comments.Text: 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...
Comments.UserDisplayName: Merc
Tags: <plugin-development><queries><createcommand>
Title: Anyone have a createCommand example?
ThreadText: # 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](http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder), 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 @username_2, 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.  
  
  ---
  
  