title: MySQL PHP Query placed within another query's while loop failing
body: <p>I have a column of pony names, where the breeder's Prefix is included with the pony's name (eg. Ashbrook Boy, where Ashbrook is the Breeder's Prefix, and Boy is the name of the pony). I have another table where I have a list of all the Prefixes used. I want to cycle through that list, and for each record search through my ponies and fetch those whose names begin with each prefix in turn. When they are fetched, I want to remove the said prefix from their name, and pop it into a column for that purpose on their own table.</p>
  
  <p>In the end, I want -rather than one column with both Prefix and Name mixed in - two columns: one for Prefix, one for Name.</p>
  
  <p>I thought the code below would do it for me, but it's not working. I get a 'not a valid resource' error for $res. Any help you could give me would be hugely appreciated - I really don't want to do this by hand! :P</p>
  
  <p>I'm using a PHP script off a MySQL db, which I can access via PHPMyAdmin.</p>
  
  <pre><code>include '../conn.php';
  
  $q=mysql_query("SELECT DISTINCT Pre FROM prefixes");
  while($r=mysql_fetch_array($q)) {
      $pre=$r['Pre'];
      $sql="SELECT ID, Name FROM profiles WHERE (Name REGEXP '^$pre') ORDER BY ID ASC";
      mysql_query($sql);
      echo $sql;
      while($res=mysql_fetch_array($sql)){
          $name=$res['Name'];
          $name=trim(str_replace("$pre","", $name));
          $id=$res['ID'];
          mysql_query("UPDATE profiles SET Prefix = '$pre', Name = '$name' WHERE ID = '$id' ");
      }
  }
  mysql_close($con);
  </code></pre>