PHP从特定MySQL行获取数据

时间:2015-01-14 06:05:39

标签: php mysql

最近我已经设置好了,所以我可以通过我的管理面板更新我的网站新闻,现在我一直在网上寻找打印特定行的解决方案。

$query = "SELECT * FROM BananzaNews";
$result = mysql_query("SELECT * FROM BananzaNews", $con);
$num_rows = mysql_num_rows($result);

上面告诉我我有多少行以及我最近的五行将在我的主页上显示,我想获取$ num_rows,$ num_rows -1,$ num_rows -2,$ num_rows的数据 - 3和$ num_rows -4。

要获取的数据;

  • 标题
  • newsDate
  • 的ImagePath
  • 含量
  • websitePath

我当前的虚拟填充代码是以下dubplicated五次;

<div class="Feed">
    <div class="Img"><img alt="" src="http://www.davidreneke.com/wp-content/uploads/2013/12/News-Briefs.png"></div>
    <div class="Title">My Testing Title</div>
    <div class="Date">
        <div class="Day">22</div>
        <div class="Month">Jan</div>
        <div class="Year">2015</div>
    </div>
    <div class="Brief">Vivamus consectetur et sapien vel rhoncus. Maecenas gravida posuere hendrerit. Duis lobortis justo ac justo malesuada commodo. Proin iaculis, erat ac aliquet eleifend, neque felis tristique turpis, at dictum tellus lectus et lorem. Nunc turpis enim, auctor sed purus nec.</div>
    <div class="More"></div>
    <div class="Divider"></div>
</div>

我的PHP

<?php
    $query = 'SELECT title,newsDate,imagePath,content,websitePath FROM BananzaNews ORDER BY id Desc LIMIT 5';
    $resultSe = mysql_query($query, $con);

    if (mysql_num_rows($result) > 0) {
        while($row = $resultSet->fetchRow ( DB_FETCHMODE_OBJECT )){
            echo "<div class=\"Feed\">
            <div class=\"Img\"><img alt=\"\" src=\"" $row->imagePath "\"></div>
            <div class=\"Title\">" $row->title "</div>
            <div class=\"Date\">
                <div class=\"Day\">" $row->newsDate "</div>
                <div class=\"Month\">" $row->newsDate "</div>
                <div class=\"Year\">" $row->newsDate "</div>
            </div>
            <div class=\"Brief\">" $row->content "</div>
            <div class=\"More\">" $row->websitePath "</div>
            <div class=\"Divider\"></div>
        </div>";
        }
    }
?> 

数据库视图 enter image description here

4 个答案:

答案 0 :(得分:1)

如果你想获取数据来获取在while循环中运行你的查询

$result=mysqli_query("select * from table_name order by id desc limit 0,5")
while($each=mysqli_fetch_array($result ))

 {
 //your code goes here
 }

答案 1 :(得分:1)

使用此查询它将适合您

$query = 'SELECT title,newsDate,imagePath,content,websitePath FROM BananzaNews ORDER BY id Desc LIMIT 5';
$resultSet = mysql_query($query);

if (mysql_num_rows($query) > 0) {
     while($row = mysql_fetch_array($resultSet)){
      echo $row['title'];
      echo $row['newsDate'];
    }
}

您可以像这样循环数据。

答案 2 :(得分:1)

您想使用MySQL的ORDER BY。您无需使用num_rows来获取最新的5篇帖子。只需按id列排序,然后LIMIT将其排序为5.如果他们没有id列,则他们应该有一个auto_increment s的id列一个PRIMARY KEY。有了这个,这段代码将帮助您:

//Load your mysql_query into a variable SELECTing not *, but only the rows you need.
$query = mysql_query("SELECT title, newsDate, imagePath, content, websitePath FROM BananzaNews ORDER BY newsid DESC LIMIT 5") or die(mysql_error()); //<- Error handling.

//This loads up each row into an associative array into the variable $row
//So, $row['title'] will be equal to the 'title' column at that row in your database
while($row = mysql_fetch_array($query)){

    //Output your repeating code
    //Notice how I've used concat operators (dots) to stop the echo and start it again.
    //I'd recommend looking those up if you haven't.
    echo 
    '<div class="Feed">
        <div class="Img"><img alt="" src="'.$row['imagePath'].'"></div>
        <div class="Title">'.$row['title'].'</div>
        <div class="Date">
            <div class="Day">22</div>
            <div class="Month">Jan</div>
            <div class="Year">2015</div>
        </div>
        <div class="Brief">'.$row['content'].'</div>
        <div class="More"></div>
        <div class="Divider"></div>
    </div>';

}

我实际上也会使用mysqli而不是mysql,主要是因为它很好... mysql-improved。请仔细研究一下,我强烈推荐它。更好的是,使用PDO

我希望这有用!

答案 3 :(得分:0)

试试这个会起作用:

1.使用mysqlipdo代替mysql分机。它已经折旧,将来会被删除。

2. Loop您的result set获取表格中的所有数据。

<?php
    $result = mysqli_query($con,"SELECT * FROM BananzaNews LIMIT 0,5");
    $rs = mysqli_fetch_array($result);
    ?>
    <table>
    <?php
    do
    {
    ?>
    <tr>
    <td><?php echo $rs['title']; ?></td>
    <td><?php echo $rs['newsDate']; ?></td>
    <td><?php echo $rs['imagePath']; ?></td>
    <td><?php echo $rs['content']; ?></td>
    <td><?php echo $rs['websitePath']; ?></td>
    </tr>
    <?php
    }while($rs = mysqli_fetch_array($result));
    ?>
    </table>