Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    query looping data twice

    I've got a variable set by sessions. Then I do a query based on that variable, but if there is more than one record, it loops through, printing the data twice?? Am I missign something in how I have the while statement below?


    Code:
    <?php
    
    	foreach($_SESSION['workshops'] as $key=>$value)
        {
    	
    	$query2 = mysql_query("SELECT tbl_attendees.attendee_fname, tbl_attendees.attendee_lname FROM tbl_attendees
    WHERE tbl_attendees.workshop_id = $value AND tbl_attendees.attendee_registrationid = $rid");
    	
    	    while ($row2 = mysql_fetch_array($query2)) {
    
    	    echo "workshop ".$row2['workshop_id']." - ".$row2['attendee_fname']." &nbsp;". $row2['attendee_lname']."<br>";
        // and print out the values
    	    }
    
        }
        ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You mean if a duplicate exists in $value? Then yes, it will run twice.
    Using an IN clause will get around that. If two values match on a where clause with the same criteria, only one record is returned. You can pull unique as well.
    PHP Code:
    $sIn implode(', 'array_unique(array_values($_SESSION['workshops'])));
    $query2 mysql_query("SELECT tbl_attendees.attendee_fname, tbl_attendees.attendee_lname FROM tbl_attendees
    WHERE tbl_attendees.workshop_id IN ($sIn) AND tbl_attendees.attendee_registrationid = $rid"
    ); 
    I don't see where $rid is, so I assume its been cut out.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •