Jump to content

[SOLVED] Getting blank strings from an sql querry


SyncViews

Recommended Posts

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>';
}

 

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.

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)

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++;
}

?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.