2011-09-12 13 views
0

if($comments_count == 0)if($comments_count == 1)으로 변경하면 텍스트가 1 개있는 경우 해당 텍스트가 반복됩니다. 그러나 == 0으로 되돌아 가면 명령이 실행되지 않습니다. 코멘트가없는 페이지에서 $ comments_count의 값을 반향 출력하려고 시도했지만 0으로 표시됩니다.하지만 if-else는이를 무시하고 아무 것도 출력하지 않습니다.값이 0 일 때 if 문이 작동하지 않는 경우 (PHP)

$query = "SELECT * FROM comments WHERE id = {$set_id}"; 
    $all_comments_set = mysql_query($query); 
    $comments_count = mysql_num_rows($all_comments_set); 

    while($comment = mysql_fetch_array($all_comments_set)) { 
     if($comments_count == 0) { 
      echo $comments_count; 
      echo "<div class='comments'><p>There are no comments to show right now. Be the first to comment!</p></div>"; 
     } else { 
      echo $comments_count; 
      echo "<div class='comments'> 
       <p class='commenter'><a href=''>".$comment['commentAuthor']."</a></p> 
       <p>".$comment['comment']."</p> 
       </div>"; 
     } 
    } 
+1

분명히 댓글이 없다는 것을 알고 있다면 확실히 while 루프를 반복하고 싶지 않을 것입니다. –

+0

사실, 그렇게 이해할 수 있습니다. – catandmouse

답변

3

당신은 while 루프 밖으로 if 문을 이동해야합니다

는 코드입니다. 행 수가 0이기 때문에 mysql_fetch_array 호출은 결과를 반환하지 않고 내부 while 루프 코드는 실행되지 않습니다. 당신은 당신이 정말로 prepared 제표 및 mysqli 사용으로 전환 또는 적어도 SQL 주입 공격을 방지하기 위해 mysql_real_escape_string를 사용하여 입력을 탈출해야 할 경우 보조 노트로

if($comments_count == 0) { 
     echo $comments_count; 
     echo "<div class='comments'><p>There are no comments to show right now. Be the first to comment!</p></div>"; 
} else { 
    while(....){ 
    } 
} 

.

0

아마도 행이 없으면 mysql_fetch_array는 false를 반환 할 것입니다.

관련 문제