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

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.