2014-02-12 13 views
0

여기 날짜로 검색 할 때이 프로젝트를 만들고 있습니다. 결과가 없으면 no data found을 출력하고 싶습니다. 그러나 결과가 없으면 여기서 표 형식을 얻습니다. 데이터가 표시되면 OK! 하지만 문제는 데이터베이스에 데이터가없는 경우입니다.출력은 데이터가 없어야합니다.

<?php 
include('connect.php'); 
    ?> 
    <?php 
// echo $res; 
$result = mysqli_query($con,"SELECT * FROM buyer where date='$res'"); 
//$result = mysqli_query($con, "SELECT * FROM buyer WHERE date='09-01-14'"); 

if($result==NULL) 
{ 
    echo "no data found"; 
} 
else{ 
echo "<table class='CSSTable'> 

<tr> 
<th>invoice no</th> 
<th>Buyer Name</th> 
<th>Buyer Order number</th> 
<th>Date</th> 
<th>Total Amount</th> 
<th>Total Items</th> 
<th>Generate PDF</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
echo "<td>" . $row['invoice_number'] . "</td>"; 
    echo "<td>" . $row['buyer_name'] . "</td>"; 
    echo "<td>" . $row['buyer_order_number'] . "</td>"; 
    echo "<td>" . $row['date'] . "</td>"; 
    echo "<td>" . $row['total_amount'] . "</td>"; 
    echo "<td>" . $row['total_items'] . "</td>"; 
    echo "<td>" ." <a href='get_pdf.php?id={$row['invoice_number']}' target='_blank'><img src='image/download.png' width='16' height='16' /></a>" . "</td>"; 
    echo "</tr>"; 
    } 

echo "</table>"; 
} 

mysqli_close($con); 
?> 
+0

시도'$ NUM = mysqli_num_rows 같은 결과의 행 수를 계산하는 mysqli_num_rows()을 사용할 수 있습니다 $ result);'$ num> 0인지 확인하십시오. – krishna

+0

http://www.php.net/manual/en/mysqli-result.num-rows.php –

+0

... 감사합니다! – Smoke

답변

1

num_rows을 사용하여 결과에서 반환 된 행 수를 확인해야합니다. 이렇게 :

if(!$result->num_rows) 
{ 
    echo "no data found"; 
} 

num_rows은 항상 반환되는 정수 값의 행을 반환합니다.

0

하는 것은 당신이이 같이 보이는 라인 mysqli_query라고 있으리라 믿고있어 :

$result = mysqli_query($query); 

mysqli_query 쿼리을 실행하는 동안 실패 만 반환 false 경우 것입니다. 쿼리가 성공적으로 실행되고 선택된 행이없는 경우 false을 반환하지 않습니다 (경우에 따라). mysqli_queryfound here에 대한 설명서를 참조 할 수 있습니다.

대신에 성공적으로 실행 된 쿼리의 $result에 데이터 행이 있는지 확인해야합니다. 이렇게하려면 mysqli_num_rows($result)으로 전화하십시오. 이렇게하면 쿼리에서 반환 한 행 수가 반환됩니다. 이것이 0과 같으면 처리 할 데이터가없는 시나리오입니다.

0
행의 수를 확인하여

시도 mysqli_num_rows()

$num = mysqli_num_rows($result); 

if($num == 0) 
{ 
echo "no data found"; 
} 
else 
{ 
//do something 

} 
0

를 사용하여 반환 당신은 (다음

if(mysqli_num_rows($result)>0){ 
//there are records 
} 
else{ 
//no records found 
}