2012-08-03 5 views
4

mysql_fetch_array() 함수가 빈 배열을 반환하는지 확인하려고합니다. 하지만 내 코드가 작동하지 않는 것 같습니다. 여기서 배열이 비어 있다면 구성 메시지 아래에 표시하고 싶습니다.가져온 어레이가 비어 있거나 PHP가 아닌지 확인하십시오.

코드 :

$queryContents= queryMembers(); 
$exeQuery = mysql_query($queryContents); 
while($fetchSet = mysql_fetch_array($exeQuery)) { 
    if(count($fetchSet) == 0) { 
    echo "This Page is Under Construction"; 
    }else{ 
    // something else to display the content 
    } 
} 

어떻게 같은 기능을 acheive 확인합니까?

+0

$ fetchSet에있는 값은 무엇입니까? (시도 var_dump() 및 검사) –

답변

13

행 수를 계산하려면 mysql_num_rows를 사용하십시오. 이 시도.

$exeQuery = mysql_query($queryContents); 

if(mysql_num_rows($exeQuery)== 0){ 
    echo "This Page is Under Construction"; 
} 
else{ 
    while($fetchSet = mysql_fetch_array($exeQuery)) { 

    // something else to display the content 

    } 
} 
+0

이 작품을 주셔서 감사합니다 – monk

0

if(empty($fetchSet) 
{ 
    echo "This Page is Under Construction"; 
} 
else 
{ 
// something else to display the content 
} 
1

당신은 이런 식으로 할 수보십시오 :

while($r[]=mysql_fetch_array($sql)); 
// now $r has all the results 
if(empty($r)){ 
    // do something 
} 

소스 : 당신이로 MYSQL_ASSOC()를 사용하는 경우 php doc

4

는, 그것은에서 행을 반환 while 루프를 사용할 때 하나씩 데이터 을 설정하십시오.

루프가 실행되지 않는 동안 레코드가없는 경우. 이 경우 부울 변수를 선언하고 while 루프로 들어가면 true로 만듭니다. 마찬가지로이 작동

$queryContents= queryMembers(); 
$exeQuery = mysql_query($queryContents); 
$recordExists = 0; 
while($fetchSet = mysql_fetch_array($exeQuery)) { 
    if($recordExists == 0) 
     $recordExists = 1; 
    // something else to display the content 

} 

if($recordExists == 0){ 
    echo "This Page is Under Construction"; 
} 

희망!

+0

결과가 없다면 루프를 실행하는 지점은 무엇입니까? – pahan

+0

당신은 절대적으로 옳습니다. 이는 또 다른 구현이었습니다. 그러나, 좋은 당신! 또는 구체적으로, 당신의, 오른쪽! –

+0

나는 나의 대답이 이것을하는 유일한 방법이라고 말하려고하지 않고있다. 여기에 행 수가 0이면 while 루프를 수행 할 필요가 없습니다. 그래서 내부 다른 부분을 갖는 것이 더 효율적입니다. – pahan

0

결과가없는 경우 while 루프 내의 코드가 실행되지 않습니다. mysql_fetch_array는 결과가 더 이상 없다면 null/false를 반환한다. 당신이해야 할 일은 mysql_num_rows를 먼저 체크하는 것입니다.

$queryContents= queryMembers(); 
$exeQuery = mysql_query($queryContents); 

if(mysql_num_rows ($exeQuery) == 0) { 
    echo "This Page is Under Construction"; 
} 

while($fetchSet = mysql_fetch_array($exeQuery)) { 
    // something else to display the content 
} 
+1

결과가없는 경우 루프를 실행하는 시점은 무엇입니까? – pahan

관련 문제