0

I have more than one article stored in my database, that I want to display. However, only the most recent article is being displayed on the page, rather than all of them. Any help to show all the articles would be appreciated.

 <div class="column-center">

     <?php
                 include 'includes/connect.php';

   $select_news = "select * FROM news";

   $run_news = mysql_query($select_news);

   while ($row_news=mysql_fetch_array($run_news)){
       $article_id = $row_news['article_id'];
       $article_title = $row_news['article_title'];
       $article_date = $row_news['article_date'];
       $article_author = $row_news['article_author'];
       $article_keywords = $row_news['article_keywords'];
       $article_content = $row_news['article_content'];
       $article_image = $row_news['article_image'];


   }

   echo "

    <a href='details.php?article=$article_id'>$article_title</a>

    <div>$article_content <a href='article.php?article=$article_id'>Read More</a></div><br />

    <img src='images/$article_image' width='100' height='100'/>




    ";

?>
</div>
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Ash
  • 73
  • 1
  • 1
  • 7
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 15 '15 at 22:23
  • Because when you're setting those variables in your `while` loop, you're __overwriting__ the values that you set for the previous record – Mark Baker Dec 15 '15 at 22:23
  • Move the echo() up into the loop, just before the ending brace. :D – HoratioCain Dec 15 '15 at 22:31

2 Answers2

1

Put your output in your while loop so that each record gets placed in the page:

while ($row_news=mysql_fetch_array($run_news)){
       $article_id = $row_news['article_id'];
       $article_title = $row_news['article_title'];
       $article_date = $row_news['article_date'];
       $article_author = $row_news['article_author'];
       $article_keywords = $row_news['article_keywords'];
       $article_content = $row_news['article_content'];
       $article_image = $row_news['article_image'];




   echo "<a href='details.php?article=$article_id'>$article_title</a>
         <div>$article_content <a href='article.php?article=$article_id'>Read More</a></div><br />
         <img src='images/$article_image' width='100' height='100'/>";

}

You should stop using mysql_* functions. These extensions have been removed in PHP 7. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.

You're also assuming your queries work all of the time and that is just a bad plan. Add error checking, such as or die(mysql_error()) to your queries. Or you can find the issues in your current error logs.

Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

you should make echo in while loop, so your code should be:

while ($row_news=mysql_fetch_array($run_news)){
       $article_id = $row_news['article_id'];
       $article_title = $row_news['article_title'];
       $article_date = $row_news['article_date'];
       $article_author = $row_news['article_author'];
       $article_keywords = $row_news['article_keywords'];
       $article_content = $row_news['article_content'];
       $article_image = $row_news['article_image'];

 echo "

    <a href='details.php?article=$article_id'>$article_title</a>

    <div>$article_content <a href='article.php?article=$article_id'>Read More</a></div><br />

    <img src='images/$article_image' width='100' height='100'/>




    ";

   }
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38