0

请有人向我解释一下我如何调整我的代码来制作它,以便如果 mysql 表中不存在记录/值,它将回显一段文本?谢谢你。

<?php

$reviews_set = get_reviews();

 ?>
<h3>Latest Reviews</h3>
<?php
while ($reviews = mysql_fetch_array($reviews_set)) {
 ?>
 <div class="prof-content-box" id="reviews">
 <div class="message_pic">
 <?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\"  src=\"{$prof_photo}\"></a>";?>
 <?php echo "<strong>Review from  {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']}     <br />";


  ?>
4

5 回答 5

1

使用三元运算符 '?:'

样本:

$you_var ?: 'you_text_if_not_exists'
于 2012-10-25T11:53:23.060 回答
1

像这样检查你的变量:

<?php (isset($reviews['display_name']) ? $reviews['display_name'] : "entry doesn't exists"; ?>
于 2012-10-25T11:53:25.587 回答
0
<?php
    $reviews_set = get_reviews();
    ?>
    <h3>Latest Reviews</h3>
    <?php
    if(mysql_num_rows($reviews_set) > 0) {
        while ($reviews = mysql_fetch_array($reviews_set)) {
            ?>
            <div class="prof-content-box" id="reviews">
             <div class="message_pic">
             <?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\"  src=\"{$prof_photo}\"></a>";?>
             <?php echo "<strong>Review from  {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']}     <br />";
        <? 
        }
    }else{
      echo "No Data";
    }

    ?>

我希望这有帮助

于 2012-10-25T11:59:54.030 回答
0
    <?php

     $reviews_set = get_reviews();

   ?>
   <h3>Latest Reviews</h3>
   <?php
    if(mysql_num_rows($reviews =  mysql_fetch_array($reviews_set))>=1)
    {
    while ($reviews = mysql_fetch_array($reviews_set)) {
    ?>
    <div class="prof-content-box" id="reviews">
    <div class="message_pic">
    <?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\"  src=\"{$prof_photo}\"></a>";?>
    <?php echo "<strong>Review from  {$reviews['display_name']}:</strong><br /><br/>    {$reviews['content']}     <br />";
    }
    } else {
    echo 'No reviews available';
    }


    ?>
于 2012-10-25T11:55:37.437 回答
0

如果要检查表是否有行,请使用以下内容:

$num_rows = mysql_num_rows($reviews_set);

$num_rows将包含行数。

于 2012-10-25T11:55:43.513 回答