Reputation: 11
I'm using the same while loop on two pages where the only difference is a different SQL call on each page. The first works as it should, grabbing the contents from the query and displaying it in an image slider:
<?php
$qry = mysql_query(sprintf("SELECT * FROM manageme WHERE element IN ('%s')", $element));
$i = 0;
while (($t = mysql_fetch_array($qry)) != null && $i < 52) { $i++;?>
<li>
<a href="">
<img src="<?php print $t['fileLocation']; ?>";
title="<?php print $t['element_name']; ?>";
name="<?php print $t['clickLocation']; ?>";
alt="wat"
class="btnshow" />
</a>
</li>
<?php } ?>
This works as it should stepping through the loop and grabbing the contents from the query so long as there are less than 52 entries returned, ending when each record has displayed once.
Upon implementing the same while loop with a different SQL call, the problem occurs.
<?php
$qry = mysql_query(sprintf("SELECT u.username, us.user_id, us.service_id,
s.imageID, s.title, s.clickLocation, s.fileLocation, s.element_name
FROM user u, user_service us, manageme s
WHERE us.user_id = %s
AND s.imageID = us.service_id", $_COOKIE['user_id']));
$i = 0;
while (($t = mysql_fetch_array($qry)) != null && $i < 52) { $i++;?>
<li>
<a href="">
<img src="<?php print $t['fileLocation']; ?>";
title="<?php print $t['element_name']; ?>";
name="<?php print $t['clickLocation']; ?>";
alt="wat"
class="btnshow" />
</a>
</li>
<?php } ?>
Instead of grabbing from the array and ending the while loop when the contents have displayed once, it continues to loop the contents in the image slider until 52 entries have been displayed. So for instance, if the SQL call only finds 4 records to display in the image slider, the set of 4 displays 13 times (52/4) before ending the loop.
Can anyone help me figure out how to end this loop after it has displayed each record from my database once and only once? Thanks in advance!
Upvotes: 1
Views: 1173
Reputation: 5239
Try adding LIMIT 52
to your query, and do the while loop as,
while ($t = mysql_fetch_array($qry)) {
//do something
}
Upvotes: 1
Reputation: 2597
Get rid of the $i < 52
check. Instead add LIMIT 52
to the end of your SQL query. Then change the while loop to:
while ($t = mysql_fetch_array($qry)) {
....
}
Upvotes: 0
Reputation: 4514
From the manual it seems that your call to mysql_fetch_array($qry)
will never return null. At the end of the result set you will get a false
return. If you change your conditional to check for this you should be able to terminate the loop
Upvotes: 0
Reputation: 7449
use mysql_num_rows($qry)
to terminate the loop in addition to < 52
Something like:
(($t = mysql_fetch_array($qry)) != null && ($i < mysql_num_rows($qry) || $i < 52))
Upvotes: 0
Reputation: 360572
Major problems:
1) Vulnerable to SQL injection attacks. Enjoy having your server destroyed.
2) Terminology: you're not fetching "from an array". You're fetching a row of data from a result set, returning that row AS an array.
3) Your loop should terminate after 52 records OR no more records are avaialble, even though you're using some ugly syntax in the while() condition. Check that your query is actually fetching the right records, in the order/format you're expecting. There's nothing in your code that'd cause PHP to output "duplicate" rows.
Upvotes: 0