2015-12-15 122 views
0

我有多个文章存储在我的数据库中,我想要显示。但是,只有最近的文章才显示在页面上,而不是全部。任何帮助显示所有文章将不胜感激。为什么我的所有记录都没有显示?

<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> 
+1

请[停止使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 [这些扩展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[编写]​​(http://en.wikipedia.org/ wiki/Prepared_statement)语句[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart .prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+0

因为当你在'while'循环中设置这些变量时,你是__overwriting__你为前一个记录设置的值 –

+0

将echo()上移到循环中,就在结束括号之前。 :d – HoratioCain

回答

0

你应该回响在while循环,使你的代码应该是:

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'/> 




    "; 

    } 
3

把你的输出while循环,使每一条记录被放置在页面:

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'/>"; 

} 

您应该stop using mysql_* functionsThese extensions已在PHP 7中删除。了解有关PDOMySQLiprepared对帐单,并考虑使用PDO,it's really pretty easy

你也假设你的查询一直工作,这只是一个糟糕的计划。向您的查询添加错误检查,例如or die(mysql_error())。或者您可以在当前的错误日志中找到问题。

相关问题