从表中选择all并显示结果mysql

时间:2015-05-11 16:13:43

标签: php mysql

<?php
   $query_announcement = mysql_query("SELECT * FROM announcement");
   $get_announcement = mysql_fetch_assoc($query_announcement);
?>

  <?php
        if(mysql_num_rows($query_announcement) > 0){
            $row = mysql_num_rows($query_announcement);

               for($x = 1; $x<= $row; $x++){

               $html = '<tr>';
               $html .= '<td>'.$get_announcement['id'].'</td>';
               $html .= '<td>'.$get_announcement['announce_name'].'</td>';
               $html .= '<td>'.$get_announcement['announce_description'].'</td>';
               $html .= '<td>'.$get_announcement['announce_location'].'</td>';
               $html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_start'])).'</td>';
               $html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_end'])).'</td>';
               $html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_added'])).'</td>';
               $html .= '<td width="15%">

              <button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
              <button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
              </td>';
              $html .= '</tr>';

              echo $html;
              }
           }
               else{
                echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
              }
    ?>

/这是我的代码,但是当我运行它时,我只获得具有相同ID的公告,而不是我创建的所有公告。请帮助谢谢

3 个答案:

答案 0 :(得分:1)

您正在循环播放公告,但之后不会撤消下一个公告。

for($x = 1; $x<= $row; $x++){
//Pull next record
$row = mysql_num_rows($query_announcement);

如果将其更改为上面的代码,则for循环将开始。数据将被拉出,显示,然后再循环回来。

答案 1 :(得分:0)

你的r只获取一次数据

for($x = 1; $x<= $row; $x++){
$get_announcement= mysql_fetch_assoc($query_announcement);

答案 2 :(得分:0)

尝试此操作:您需要遍历行并使用每一行来显示它们。

<?php
   $query_announcement = mysql_query("SELECT * FROM announcement");
   $get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php


 if($get_announcement){

  while($row = mysql_fetch_array($get_announcement)) {

               $html = '<tr>';
               $html .= '<td>'.$row['id'].'</td>';
               $html .= '<td>'.$row['announce_name'].'</td>';
               $html .= '<td>'.$row['announce_description'].'</td>';
               $html .= '<td>'.$row['announce_location'].'</td>';
               $html .= '<td>'.date('M d,Y', strtotime($row['date_start'])).'</td>';
               $html .= '<td>'.date('M d,Y', strtotime($row['date_end'])).'</td>';
               $html .= '<td>'.date('M d,Y', strtotime($row['date_added'])).'</td>';
               $html .= '<td width="15%">

              <button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
              <button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
              </td>';
              $html .= '</tr>';

              echo $html;
              }
           }
               else{
                echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
              }