This is a stand alone script, so this is all the code! It writes the data associated with the first Projects record and that is it, what am I missing? <?php $conn = mysql_connect("xxx","xxx","xxx"); mysql_select_db("xxx",$conn); $OutputFile = fopen('Orders.txt','w'); $SQL="Select ProjectID From Projects"; $ProjectInfo = mysql_query($SQL); while ($Info = mysql_fetch_array($ProjectInfo)) { $SQL="Select FileName From PreviewPhoto Where ProjectID = ".$Info['ProjectID']; $PreviewPhoto = mysql_query($SQL); while ($Photo = mysql_fetch_array($PreviewPhoto)) { fwrite($OutputFile, $Photo['FileName'].", "); $SQL = "Select Text From PreviewText Where ProjectID = ".$Info['ProjectID']; $PreviewText = mysql_query($SQL); while ($Text = mysql_fetch_array($PreviewText)) { fwrite($OutputFile, $Text['Text'].", "); } fwrite($OutputFile,"\n"); } } fclose($OutputFile); ?> PHP:
Using your code I do get multiple records. I guessed at the table structures, but from your queries what I used should be fine. I have attached my mysqldumps of the three tables you query from below. In my tests i have three ProjectIDs and running the script correctly populates the Orders.txt file with three rows: $ cat Orders.txt testPhoto3, this is the third test, testPhoto2, this is the second test, testPhoto1, this is the first test, Code (markup): Just a guess, but maybe your "Projects" table only has one row containing a ProjectID? Here are dumps of my test tables: -- -- Table structure for table `Projects` -- CREATE TABLE Projects ( ProjectID int(5) default NULL ) TYPE=MyISAM; -- -- Dumping data for table `Projects` -- INSERT INTO Projects VALUES (3); INSERT INTO Projects VALUES (2); INSERT INTO Projects VALUES (1); Code (markup): -- -- Table structure for table `PreviewPhoto` -- CREATE TABLE PreviewPhoto ( ProjectID int(5) default NULL, FileName varchar(50) default NULL ) TYPE=MyISAM; -- -- Dumping data for table `PreviewPhoto` -- INSERT INTO PreviewPhoto VALUES (1,'testPhoto1'); INSERT INTO PreviewPhoto VALUES (2,'testPhoto2'); INSERT INTO PreviewPhoto VALUES (3,'testPhoto3'); Code (markup): -- -- Table structure for table `PreviewText` -- CREATE TABLE PreviewText ( ProjectID int(5) default NULL, Text varchar(50) default NULL ) TYPE=MyISAM; -- -- Dumping data for table `PreviewText` -- INSERT INTO PreviewText VALUES (3,'this is the third test'); INSERT INTO PreviewText VALUES (2,'this is the second test'); INSERT INTO PreviewText VALUES (1,'this is the first test'); Code (markup):