2013-04-27 3 views
0

안녕하세요 저는이 주제에 대한 연구를 해봤지만 몇 가지 해결책을 찾아 냈습니다. 초보자이기 때문에 코드를 구현할 수 없었습니다. 내 질문은 기본적으로 값이 MySQL 데이터베이스에서 발견되지 않으면 어떻게 메시지를 표시 할 수 있습니까?값을 찾지 못하면 오류 메시지가 표시됩니다. mysql

는 이전에 검색 : Displaying message when no results found in PHP MySQL searchmysql fetch array if no results display message

 <?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
</head> 
<body> 
<?php 
$customer = $_GET["custID"]; 
$conn = mysql_connect("localhost", "localuser", "pass"); 
mysql_select_db("testdb", $conn) 
or die ('Database not found ' . mysql_error()); 
$sql = "SELECT orderNumber, customerID, orderDate, shippingDate, shipped FROM orders where customerID = $customer ORDER by orderDate"; 
$rs = mysql_query($sql, $conn) 
or die ('Problem with query' . mysql_error()); 
?> 
<table border="1" summary="Customer Details"> 
<tr> 
<th>Order Number</th> 
<th>Customer ID</th> 
<th>Order Date</th> 
<th>Shipping Date</th> 
<th>Shipped</th> 
</tr> 
<?php 
     $results = mysql_fetch_array($rs); 
     if ($results === FALSE) 
     { 
      echo "No result"; 
     } 
     else 
     { 
      foreach($results as $item) 
      {?> 
       <tr> 
        <td><?php echo $result["orderNumber"]?></td> 
        <td><?php echo $result["customerID"]?></td> 
        <td><?php echo $result["orderDate"]?></td> 
        <td><?php echo $result["shippingDate"]?></td> 
        <td><?php echo $result["shipped"]?></td> 
       </tr> 
      <?php } 
     } 
mysql_close($conn); ?> 
</table> 
</body> 
</html> 
+0

당신이 그들을 이해하지 않기 때문에, 당신이 ' 세 번째 질문은? 이 과정은 오래 걸릴 것입니다. 지금까지 해왔 던 대답을 이해하려고 노력해야한다고 생각하지 않습니까? –

+0

머지 않아 프로젝트를 빨리 끝내는 것이 좋습니다. mysql 확장은 PHP 5.5에서 더 이상 사용되지 않는 경고를 던지며 PHP 5.6에서는 제거 될 것입니다. –

답변

0

사용 mysql_num_rows()

if(mysql_num_rows($rs) > 0) { 
    // got records 
} 
else { 
    // no records found 
} 

참고 :이되지 가고 있기 때문에

는 mysql_로 * 가족 기능을 사용하지 마십시오. mysqli 또는 pdo를 살펴보기 시작하십시오.

+0

올바른 값을 입력해도 데이터가 표시되지 않는 경우 코드가 업데이트되었지만 이유는 무엇입니까? – Matin

0

이 용도 이 잘못된 경우. mysql_fetch_row 결과가 없을 때 항상 FALSE를 반환합니다. 참조하십시오 http://php.net/manual/en/function.mysql-fetch-array.php

당신은 언젠가처럼 사용해야하는

<?php 
     $results = mysql_fetch_array($rs); 
     if ($results === FALSE) 
     { 
      echo "No result"; 
     } 
     else 
     { 
      foreach($results as $item) 
      { 
        ... 

      } 
     } 
    ?> 
+0

같은 결과가 나오면 @ GBD에서 시도한 코드에 언급 된 것과 같은 결과가 발생합니다. 아무런 값도 반환되지 않으면 결과가 없습니다. 올바른 값을 입력하면 테이블에 데이터가 채워지지 않더라도 해당 비트에 막혀 있습니다. – Matin

+0

코드가 다시 업데이트되었습니다.이 시간 오류가 발생합니다. – Matin

+0

어떤 종류의 오류 ?? –

0
$result = mysql_fetch_array($rs) 
if($result) 

... 그래서 당신은 이미 답변의 몇 가지를 발견하고

관련 문제