SyncViews Posted December 11, 2007 Share Posted December 11, 2007 How come when I go to display the data all I get are blank strings? I don't get an sql errors and it's working well enough to know that theres only 2 topics in my news forum (eg it only draws 2 boxes rather than the max of 5) I just really don't see what Ive done wrong $result = mysql_query (" SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_first_post_id, t.topic_time, p.post_text, p.topic_id FROM phpbb_topics AS t, phpbb_posts AS p WHERE t.forum_id='$newsID' AND t.topic_first_post_id=p.topic_id ORDER BY t.topic_time ") or exit (mysql_error()); $i = 0; while ($data = (mysql_fetch_array($result) or exit (mysql_error())) && $i < 5) { $date = $data['topic_time']; $title = $data['topic_title']; $id = $data['topic_id']; $post = $data['post_text']; $i++; echo '<div class="body_news">' . '<a href="http://syncproductions.exofire.net/viewtopic.php?f=' . $newsID . '&t=' . $id . '"/>' . $title . '</a><br />' . 'Posted on: ' . $date . '<br />' . '<hr />' . $post . '</div>'; } Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 Are you exiting if $i is less than 5 here? $i = 0; while ($data = (mysql_fetch_array($result) or exit (mysql_error())) && $i < 5) since you set $i=0, $i will always be less than 5 when you hit your while. Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 11, 2007 Author Share Posted December 11, 2007 yes. Which means the loop will only run up to 5 times so that it doesn't list every news item ever. But as it happens theres only 2 news topics so the loop only runs twice (so I know the query works at least enough for it to know there is only 2 records that meet the WHERE requirements) Quote Link to comment Share on other sites More sharing options...
revraz Posted December 11, 2007 Share Posted December 11, 2007 I would use LIMIT in your query instead. Quote Link to comment Share on other sites More sharing options...
marcus Posted December 11, 2007 Share Posted December 11, 2007 Why don't you just limit the query? <?php $result = mysql_query(" SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_first_post_id, t.topic_time, p.post_text, p.topic_id FROM phpbb_topics AS t, phpbb_posts AS p WHERE t.forum_id='$newsID' AND t.topic_first_post_id=p.topic_id ORDER BY t.topic_time DESC LIMIT 5 ") or exit(mysql_error()); $i = 0; while ($data = mysql_fetch_array($result)) { $date = $data['topic_time']; $title = $data['topic_title']; $id = $data['topic_id']; $post = $data['post_text']; echo '<div class="body_news">' . '<a href="http://syncproductions.exofire.net/viewtopic.php?f=' . $newsID . '&t=' . $id . '"/>' . $title . '</a><br />' . 'Posted on: ' . $date . '<br />' . '<hr />' . $post . '</div>'; $i++; } ?> Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 11, 2007 Author Share Posted December 11, 2007 hmm..mayby cause I'm new to php/sql and I didn't know limmit existed lol... Anyway somhow that fixed it...no idea why though Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 11, 2007 Share Posted December 11, 2007 Two additions to the last post 1) remove the $i part its ambitious now 2) add an error checking. [code=php:0] <?php $q = " SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_first_post_id, t.topic_time, p.post_text, p.topic_id FROM phpbb_topics AS t, phpbb_posts AS p WHERE t.forum_id='$newsID' AND t.topic_first_post_id=p.topic_id ORDER BY t.topic_time DESC LIMIT 5"; $result = mysql_query($q) or exit(mysql_error()); if(mysql_num_rows($result)>0){ while ($data = mysql_fetch_array($result)) { $date = $data['topic_time']; $title = $data['topic_title']; $id = $data['topic_id']; $post = $data['post_text']; echo '<div class="body_news">' . '<a href="http://syncproductions.exofire.net/viewtopic.php?f=' . $newsID . '&t=' . $id . '"/>' . $title . '</a><br />' . 'Posted on: ' . $date . '<br />' . '<hr />' . $post . '</div>'; } } else{ //NO results yielded from query } ?> Since we removed the error reporting from the establishment of hte loop we should replace it above that to prevent a warning Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.