title: Set PHP variable to MySQL data in foreach loop using array
body: <p>I seem to have looked everywhere for this.</p>
  
  <p>Here is the code i use to set a variable to a piece of data from a MySQL database</p>
  
  <pre><code>$PartNo=mysql_result($result,$i,"PartNo");
  </code></pre>
  
  <p>Where </p>
  
  <pre><code> $result = mysql_query("SELECT * FROM PriceList"); 
  </code></pre>
  
  <p><code>$i = 0</code>, is added to by 1 every time my while loop restarts<br>
    PartNo is a field name in my MySQL table and also the name of the variable I want to set the data in the database to<br>
    PriceList is my database name</p>
  
  <p>I want to loop through all the field names (the array has them) and set variables with the same names to that data. Like this:</p>
  
  <pre><code>$PartNo=mysql_result($result,$i,"PartNo");
  $Group=mysql_result($result,$i,"Group");
  $OnHand=mysql_result($result,$i,"OnHand");
  $QOO=mysql_result($result,$i,"QOO");
  $Description=mysql_result($result,$i,"Desc");
  $Cost=mysql_result($result,$i,"Cost");
  </code></pre>
  
  <p>But with a foreach loop so it isn't as much code.<br>
  I was thinking something like this, but it won't execute no matter which way I go about it (parse_str, eval, exec, etc.)</p>
  
  <pre><code>$nDatabaseVars=array("PartNo","Group","OnHand","QOO","Desc","Cost");
  
  foreach ($nDatabaseVars as $X) {
      $$X=mysql_result($result,$i,'$X');
  }
  </code></pre>
  
  <p>I need "<code>$$X</code>" to evaluate out so on the first iteration, it changes to $PartNo= and then sets <code>$PartNo</code> to whatever data is in the database on the first line. Which is what this part is: <code>mysql_result($result,$i,"PartNo")</code></p>
  
  <p>If I echo it out instead:</p>
  
  <pre><code>foreach ($nDatabaseVars as $X) {
      echo "$$X=mysql_result($result,$i,'$X')";
  }
  </code></pre>
  
  <p>I can get it to say exactly what I need executed ($PartNo=mysql_result($result,$i,"PartNo");) but not actually get the variable set.</p>