title: powershell foreach loop using a MYSQL query
body: <p>Good afternoon, </p>
  
  <p>i'm trying to pull a query from Mysql and get a loop from each entry of that query.</p>
  
  <p>here's the MySQL query </p>
  
  <p><code>SELECT ad_groups FROM ad_test_table where ad_names = 'test'</code></p>
  
  <p>which returns </p>
  
  <p><code>test1,test2,test3</code></p>
  
  <p>now i want to do a for loop for each entry ad_group but it wont loop each of those separated by a coma</p>
  
  <pre><code>    foreach ($group in $AD_query){
      write-host "group : " $group
      }
  </code></pre>
  
  <p>which only loops once and gives me <code>test1,test2,test3</code></p>
  
  <p>here's the full code, hopefully someone can help me is it the data that was incorectly entered in the data base or am i missing something in the code?</p>
  
  <pre><code>function mysql_query($query)
      { 
  
  $MySQLAdminUserName = 'AD_user'
  $MySQLAdminPassword = 'AD_password'
  $MySQLDatabase = 'User_creation'
  $MySQLHost = 'AD_MySQL_server'
  $ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" + $MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword + ";database="+$MySQLDatabase
  Try {
    [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
    $Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
    $Connection.ConnectionString = $ConnectionString
    $Connection.Open()
    $Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection)
    $DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
    $DataSet = New-Object System.Data.DataSet
    $RecordCount = $dataAdapter.Fill($dataSet, "data")
    $DataSet.Tables[0]
    }
  Catch {
    Write-Host "ERROR : Unable to run query : $query `n$Error[0]"
   }
  Finally {
    $Connection.Close()
    }
    }
  
  
  
  
  #$Full_AD_rights_list_query_full = New-Object System.Collections.ArrayList
  
  $AD_query = New-Object System.Collections.ArrayList
  $AD_query = mysql_query "SELECT ad_groups FROM ad_test_table where ad_names = 'test'"
  
  
  foreach ($group in $AD_query.item(0)){
  
  write-host ""
  write-host "group : " $group
  
  }
  </code></pre>