Script only returns 1 record

Discussion in 'PHP' started by Sleeping Troll, Sep 24, 2008.

  1. #1
    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:
     
    Sleeping Troll, Sep 24, 2008 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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):
     
    drunnells, Sep 25, 2008 IP
  3. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Open the file in append mode... (line #4)

    
    fopen('Orders.txt', 'a');
    
    PHP:
     
    hamidof, Sep 25, 2008 IP