...

View Full Version : query looping data twice



turpentyne
02-27-2012, 05:16 PM
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?



<?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
}

}
?>

Fou-Lu
02-27-2012, 06:27 PM
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.


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum