如果没有正确显示结果

时间:2018-11-22 08:30:54

标签: php mysql

我要根据返回的行尝试执行if否则操作不正确,如果有人可以给我一些我做错的事,我不知道我做错了什么非常感谢您

$dbconnect=mysqli_connect($hostname,$username,$password,$db);

if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 5");
$query3=mysqli_query($dbconnect,"SELECT companyname,client_id,feedback,status from review WHERE status=3");
$rows_get = mysqli_num_rows($query);

if ($rows_get >0) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query3);
   $row3 = mysqli_fetch_assoc($query3);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
}
}else if ($rows_get >1) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query3);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
}
}else if ($rows_get >2) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
} 
}else if ($rows_get >3) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query);
   $row4 = mysqli_fetch_assoc($query);
   $row5 = mysqli_fetch_assoc($query3);
}
}else if ($rows_get >4) {
{
   $row1 = mysqli_fetch_assoc($query);
   $row2 = mysqli_fetch_assoc($query);
   $row3 = mysqli_fetch_assoc($query);
   $row4 = mysqli_fetch_assoc($query);
   $row5 = mysqli_fetch_assoc($query);
} 
}else {
   $row1 = mysqli_fetch_assoc($query3);
   $row2 = mysqli_fetch_assoc($query3);
   $row3 = mysqli_fetch_assoc($query3);
   $row4 = mysqli_fetch_assoc($query3);
   $row5 = mysqli_fetch_assoc($query3);
}

这是php / html方面,帖子被拉到这里,这是我遇到问题的地方            

    <blockquote class="blockquote" >
      <p class="mb-0">"<?php echo $row1['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row1['client_id'];  ?> @ <?php echo $row1['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote-reverse">
      <p class="mb-0">"<?php echo $row2['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row2['client_id'];  ?> @ <?php echo $row2['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote">
      <p class="mb-0">"<?php echo $row3['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row3['client_id'];  ?> @ <?php echo $row['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote-reverse">
      <p class="mb-0">"<?php echo $row4['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row4['client_id'];  ?> @ <?php echo $row4['companyname'];  ?></footer>
    </blockquote>

    <blockquote class="blockquote">
      <p class="mb-0">"<?php echo $row5['feedback']; ?>"</p>
      <footer class="blockquote-footer"><?php echo $row5['client_id'];  ?> @ <?php echo $row5['companyname'];  ?></footer>
    </blockquote>

  </div>
</div>

2 个答案:

答案 0 :(得分:0)

您可以使用while循环来代替if,而while($ row [] = mysqli_fetch_assoc($ query)); while($ row [] = mysqli_fetch_assoc($ query3)); 这两个将把您的所有数据存储在$ row数组中。无需检查条件,因为只有在存在数据并附加上一个数据的情况下,它才会获取数据。

答案 1 :(得分:0)

这可能是解决您问题的方法,即使有些棘手:

if ($dbconnect->connect_error) {
  die("Database connection failed: " . $dbconnect->connect_error);
}
$query=mysqli_query($dbconnect,"SELECT DISTINCT companyname,client_id,feedback,status from review WHERE status=1 ORDER BY RAND() LIMIT 5");
$query3=mysqli_query($dbconnect,"SELECT companyname,client_id,feedback,status from review WHERE status=3");
$rows_get = mysqli_num_rows($query);

$nq=0;

while($row=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
    if ($rows_get == 1 && $nq < 1) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 2 && $nq < 2) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 3 && $nq < 3) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 4 && $nq < 4) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else if ($rows_get == 5 && $nq < 5) {
        echo $row['companyname'];
        echo $row['client_id'];
        echo $row['feedback'];
        echo $row['status'];
        ++$nq;
    } else {
       break;
    }
}

$nq=0;

while($row3=mysqli_fetch_array($query3,MYSQLI_ASSOC))
{
    if ($rows_get == 0 && $nq < 5) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 1 && $nq < 4) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 2 && $nq < 3) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 3 && $nq < 2) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else if ($rows_get == 4 && $nq < 1) {
        echo $row3['companyname'];
        echo $row3['client_id'];
        echo $row3['feedback'];
        echo $row3['status'];
        ++$nq;
    } else {
       break;
    }
}