开发者

While loop Repeats infinitely

开发者 https://www.devze.com 2023-04-09 00:37 出处:网络
I have a while loop that populates a dropdown box with values from a mysql table. There are only two matching records and it is repeating them over and over. How do i only display each record once?:

I have a while loop that populates a dropdown box with values from a mysql table. There are only two matching records and it is repeating them over and over. How do i only display each record once?:

$query = "SELECT * FROM members, sportevents, dates, results, event, userlogin ". 
         "INNER JOIN members AS m ON userlogin.id = m.id " .
         "WHERE userlogin.username = '$un' " . 
         "AND sportevents.id = members.id " . 
         "AND sportevents.event_id = event.id " .
         "AND sportevents.date_id = dates.id " .
         "AND sportevents.result_id = results.id";

echo "<form method='POST' action='display.php'>";
echo "Please Select Your Event<br />";
echo "<select name='event'>";
echo "<option>Sel开发者_开发问答ect Your Event</option>";
$results = mysql_query($query)
    or die(mysql_error());
    while ($row = mysql_fetch_array($results)) {

    echo "<option>";
    echo $row['eventname'];
    echo "</option>";
                                               }

echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";


Have you tried running that query manually in the mysql monitor? Nothing in your code would produce an infinite loop, so most likely your query is not doing joins as you expect and is doing a cross-product type thing and creating "duplicate" records.

In particular, your query looks very suspect - you're using the lazy "from multiple tables" approach, instead of explicitly specifying join types, and you're using the members table twice (FROM members ... and INNER JOIN members). You don't specify a relationship between the original members table and the joined/aliased m one, so most likely you're doing a members * members cross-product fetch.


give that you seem to be fetching only an event name for your dropdown list, you can try eliminating the unused tables - ditch dates and results. This will simplify things considerable, then (guessing) you can reduce the query to:

SELECT event.id, event.eventname
FROM event
INNER JOIN sportevents ON event.id = sportevents.event_id
INNER JOIN members ON sportevents.id = members.id
INNER JOIN userlogins ON members.id = userlogins.id
WHERE userlogins.username = '$un'

I don't know if the members/userlogins join is necessary - it seems to just feed sportevents.id through to members, but without knowing your DB's schema, I've tried to recreate your original query as best as possible.


You could always try changing the SELECT statement to a SELECT DISTINCT statement. That'll prevent duplicates of the selected fields.
Either that or reading all the results before displaying them, then de-duping them with something like array_unique().


Checkout My Example. It will be helped for you to understand. Because this code 100% working for me. Study it and get a solution.

And You Should Use DISTINCT keyword and GROUP BY Keyword. That's the Main Thing to prevent repeating values.

    <?php
    $gtid = $_GET['idvc'];
    if(isset($_GET['idvc']))
    {   
    $sql = mysql_query("SELECT DISTINCT gallery_types.gt_id, gallery_types_category.gtc_id, gallery_types_category.gt_category, gallery_types_category.gtc_image FROM gallery_types, gallery_types_category WHERE $_GET[idvc]=gtid GROUP BY gt_category");
    mysql_select_db($database,$con);
        while($row = mysql_fetch_array($sql))
        {?>
            <table>
                <tr>

                    <td width="100px"><?php echo $row['gt_id'] ?></td>
                    <td width="100px"><?php echo $row['gtc_id'] ?></td>
                    <td width="300px"><?php echo $row['gt_category'] ?></td>
                    <td width="150px">
                        <a href='view_all_images.php?idvai=<?php echo $row[gtc_id] ?>' target='_blank'>
                            <img width='50' height='50' src='<?php echo $row[gtc_image] ?>' />
                        </a>
                    </td>
                </tr>
            </table>

        <?php 
        }   
    }
    ?>

Better Understand, Follow Following Method,

$sql = mysql_query("SELECT DISTINCT table1.t1id, table2.t2id, table2.t2name FROM table1, table2 WHERE t1id=t2id GROUP BY t2name");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号