title: Using value from query before the query executes (PHP/MySQL)
body: <p>The following query works fine - as long as the URL focuses on one of the 50 U.S. states (e.g. MySite/World/Arizona).</p>
  
  <pre><code>SELECT GG.N, GG.IDArea MyID,
   GG.Name GName, GG.FIPS, GG.NameAlph, GG.Type, GG.**IDParent**, GG.Parent, GG.IDReg, GG.Pre, GG.Post,  GG.CIA, 
  GP.IDArea GPIDArea, GP.IDParent2, GS.N, GS.IDArea SymArea, GS.URL, GS.PostURL, GS.Unique, GS.Title,   GS.Subtitle,   GS.MyKind2, GS.Symbol, GS.Latin, GS.Desig, GS.DesigGen, GS.DesigGroup, GS.Date, GS.Dupe,
  GM.Motto, GM.Language, ART.ID, ART.PostURL, ART.Article
  FROM gw_geog GG
  LEFT JOIN gs GS ON GS.IDArea = GG.IDArea
  LEFT JOIN gw_geog_par2 GP ON GP.IDArea = GG.IDArea
  LEFT JOIN gs2_motto GM ON GM.Motto = GS.Symbol
  LEFT JOIN gs_articles_world_**usa** ART ON ART.ID = GS.IDArea AND ART.PostURL = GS.PostURL
  WHERE GG.Name = :MyURL AND
  ORDER BY GS.N
  </code></pre>
  
  <p>However, it doesn't work with MySite/World/Japan. The problem is the database table gs_articles_world_usa. It's one of a series of tables, with the last three letters representing a nation state's parent. (Japan's parent in this case is Eurasia, abbreviated eur.)</p>
  
  <p>Fortunately, these parent ID's are stored in one of the tables - gw_geog, in field IDParent. So I ought to be able to change gs_articles_word_usa to gs_articles_world_$IDParent.</p>
  
  <p>The obvious problem is that the values for $IDParent - $IDParent = $row['IDParent'] - can't be pulled from the database until AFTER the query has finished executing.</p>
  
  <p>The obvious solution is to make a separate query before this query designed to extract the values for $IDParent. However, I suspect there might be some way to modify my existing query to accomplish the same thing. Any suggestions?</p>
  
  <p>P.S. Some of the values stored in field IDParent include usa, can (Canada), na (North America), sa (South America), eur (Eurasia), afr (Africa), jpn (Japan); pac, atl, ino (Pacific, Atlantic and Indian oceans) and reg (Regions).</p>
  
  <p>CREATE TABLE <code>gw_geog</code> (
   <code>N</code> mediumint(3) NOT NULL AUTO_INCREMENT,
   <code>IDArea</code> varchar(7) COLLATE latin1_general_ci NOT NULL DEFAULT '',
   <code>ISO2</code> varchar(6) COLLATE latin1_general_ci DEFAULT NULL,
   <code>ISON</code> int(3) DEFAULT NULL,
   <code>FIPS</code> char(2) COLLATE latin1_general_ci DEFAULT NULL,
   <code>Name</code> varchar(44) COLLATE latin1_general_ci NOT NULL DEFAULT '',
   <code>NameAlph</code> varchar(44) COLLATE latin1_general_ci NOT NULL DEFAULT '',
   <code>Type</code> char(3) COLLATE latin1_general_ci NOT NULL DEFAULT '',
   <code>IDParent</code> char(4) COLLATE latin1_general_ci DEFAULT NULL,
   <code>Parent</code> varchar(22) COLLATE latin1_general_ci DEFAULT NULL,
   <code>IDParent2</code> char(4) COLLATE latin1_general_ci DEFAULT NULL,
   <code>Parent2</code> varchar(22) COLLATE latin1_general_ci DEFAULT NULL,
   <code>IDReg</code> varchar(7) COLLATE latin1_general_ci DEFAULT NULL,
   <code>Pre</code> varchar(44) COLLATE latin1_general_ci DEFAULT NULL,
   <code>Post</code> varchar(44) COLLATE latin1_general_ci DEFAULT NULL,
   <code>CIA</code> tinyint(1) DEFAULT NULL,
   <code>G1</code> tinyint(1) NOT NULL DEFAULT '1',
   PRIMARY KEY (<code>N</code>),
   KEY <code>Parent</code> (<code>Parent</code>),
   KEY <code>IDReg</code> (<code>IDReg</code>),
   KEY <code>IDArea</code> (<code>IDArea</code>),
   KEY <code>IDParent</code> (<code>IDParent</code>),
   KEY <code>Name</code> (<code>Name</code>)
  ) ENGINE=MyISAM AUTO_INCREMENT=549 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci</p>