仅当查询中的数组中存在结果时,如何显示内容?

时间:2013-06-08 00:12:52

标签: php if-statement while-loop echo

如果从查询中返回结果,我将如何重写此代码以仅显示<h3>标记?

我是PHP的新手,我知道这可以做到,我只是不确定最好的方法。

我能得到的任何帮助都会很棒。

提前谢谢。

我目前的代码如下。

<h3 class="state" name="art">Art</h3>
<?php
    if (mysql_error() > "") print mysql_error() . "<br>";
    mysql_select_db($database_name, $db);
    if (mysql_error() > "") print mysql_error() . "<br>";
      $query = mysql_query("SELECT * FROM member_page WHERE community_id = $cityid AND category=1 ORDER BY entity_name");

        echo "";
      while ($row = mysql_fetch_array($query))

      {
          echo "<div id='result'><div id='entity_name'><h4>".$row['entity_name']."</h4></div><img width='275' height='170' src='images/".$row['id']."/coverphoto/".$row['cp_path']."' ><div id='searchblurb'><p>".$row['search_blurb']."</p></div><a border='0' href='member-info.php?id=".$row['id']."' onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage('go','','images/custom/go-over.png',1)'><img src='images/custom/go-out.png' alt='Visit this community' width='120' height='40' id='go' border='none'></a> 
        </div>";
      }
      echo "";
?>

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

if(mysql_num_rows($query) != 0)
{
    echo "<h3 class='state' name='art'>Art</h3>";
}

请注意mysql_* functions已弃用,请尝试学习 PDO HERE


更新

我不确定这是否是您想要的,但您可以试试这个:

if(mysql_num_rows($query) != 0)
{
    echo "<h3 class='state' name='art'>Art</h3>";
    while($row = mysql_fetch_array($query))
    {
        echo "<div id='result'><div id='entity_name'><h4>".$row['entity_name']."</h4></div><img width='275' height='170' src='images/".$row['id']."/coverphoto/".$row['cp_path']."' ><div id='searchblurb'><p>".$row['search_blurb']."</p></div><a border='0' href='member-info.php?id=".$row['id']."' onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage('go','','images/custom/go-over.png',1)'><img src='images/custom/go-out.png' alt='Visit this community' width='120' height='40' id='go' border='none'></a> 
    </div>";
  }
}