View Full Version : phpbb mysql While loop not looping

11-07-2007, 08:17 PM
I'm trying to create a portal for phpbb3 and the code below only loops once so I only get one post (yes there is actually more in the db)

if ($user->data['is_registered'])
$sql = "SELECT post_username, post_subject, forum_id, post_id FROM phpbb_posts ";
$result = $db->sql_query($sql);

while ( ($row = $db->sql_fetchrow($result)) && ($row['post_subject'] != ''))
if ($row['forum_id'] == 0 || $auth->acl_get('f_read', $row['forum_id']))
$template->assign_block_vars('new_posts', array(
'FULL_TITLE' => $row['post_subject'],
'FORUM' => $row['forum_id'],
'POSTER_NAME' => $row['post_username'],
'U_VIEW_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $row['forum_id'] . '&p=' . $row['post_id'] ),


the relevant template code is here

<!-- BEGIN new_posts -->
<a href="{new_posts.U_VIEW_TOPIC}" title="{new_posts.FULL_TITLE}">{new_posts.FULL_TITLE} by {POSTER_NAME}</a><br />
<!-- END new_posts -->

any pointer anybody I've looked at all the similar code I can find and I just can't work this out

11-07-2007, 10:24 PM
Are the post_subject column values empty? Because that's the 2nd part of the "while" loop comparison operator... the first post encountered with an empty post_subject will abort the loop.

11-07-2007, 11:47 PM
No the post_subject is always filled.

11-07-2007, 11:54 PM
Ummm. You are apparently (it is a bit hard to find the correct ending ")" due to the numerous ones floating around) freeing up the result resource inside of the while() loop. After the first time, there is no result resource and the while() loop condition is false. The following line of code should be after the end of the while() loop -


11-08-2007, 12:04 AM
that was it! perfect thanks very much :D

I spent a whole day (8+ hours) scratching my head at that one _and_ I rewrote the code three times. I can't believe I missed it