2014-09-15 5 views
0

안녕하세요! PHP 프로세스에서 "레코드 없음"메시지가 표시되는 데 문제가 있습니다.
여기 내 검색 쿼리에 대한 코드입니다 :
PHP에서 "결과를 찾을 수 없습니다"표시

if(isset($_GET['submit'])) { 

$product = $_GET['product']; 
$city = $_GET['city']; 

$query = "SELECT * FROM $product WHERE city = '$city'"; 
$result = mysqli_query($con, $query) or die ("Could not connect to database."); 
$product = str_replace('_', ' ', $product); 
$product = strtoupper($product); 
    echo "You have searched for " . $product . " in " . $city; 
    echo "<table border=1>"; 
    echo "<tr> <th>Store</th> <th>City</th> </tr>"; 
    while ($row = mysqli_fetch_array($result)) { 
    echo "<tr><td>"; 
    echo $row['store']; 
    echo "</td><td>"; 
    echo $row['city']; 
    echo "</td></tr>"; 
    } 
    echo "</table>"; 
} 

내 문제는 내가 "기록 없음"표시되지 않습니다 조건문을 배치 할 위치를 무엇을 모르는 것입니다.
누군가가이 문제에 대해 저를 도울 수 있기를 바랍니다.
미리 감사드립니다.

+1

http://php.net/manual/tr/mysqli.affected-rows.php 답변입니다. 레코드가없는 것보다 영향을받는 행이 0 개있는 경우. 간단한 :) – hakiko

+1

프로젝트에서 PDE 사용. mysqli는 거의 사용되지 않음 – hakiko

답변

2

결과의 행 수가을 통해 0인지 확인하고 결과 세트를 루핑하기 전에 메시지를 표시하십시오. mysqli_affected_rows() 기능은 이전의 선택에 영향을받는 행의 수를 반환

if(isset($_GET['submit'])) { 

    $product = $_GET['product']; 
    $city = $_GET['city']; 

    $query = "SELECT * FROM $product WHERE city = '$city'"; 
    $result = mysqli_query($con, $query) or die ("Could not connect to database."); 
    $product = str_replace('_', ' ', $product); 
    $product = strtoupper($product); 
    echo "You have searched for " . $product . " in " . $city; 
    echo "<table border=1>"; 
    // check if results are present 
    if(mysqli_num_rows($result)>0) { 
     echo "<tr> <th>Store</th> <th>City</th> </tr>"; 
     while ($row = mysqli_fetch_array($result)) { 
      echo "<tr><td>"; 
      echo $row['store']; 
      echo "</td><td>"; 
      echo $row['city']; 
      echo "</td></tr>"; 
     } 
    } else { 
     echo "<tr> <td colspan='2'> No Results found </td></tr>"; 
    } 
    echo "</table>"; 
} 
+0

이것은 마법처럼 작동합니다! 고마워요! – user3627135

+0

@ user3627135 : 문제 없습니다. :) – mithunsatheesh

0

처럼

코드가 될 수있다, INSERT, UPDATE, 대체 또는 쿼리를 삭제합니다.

참조에서

끄트머리 : http://php.net/manual/tr/mysqli.affected-rows.php

0보다 큰 정수 또는 영향 검색된 행의 수를 나타낸다. 0은 UPDATE.에 대해 갱신 된 레코드가없고, u 리의 WHERE 절과 일치하는 행이 없거나 아직 u 리가 실행되지 않았 음을 나타냄니다. -1은 쿼리가 오류를 반환했음을 나타냅니다. 오류 당신 코드의 코드는 당신이 그것을 찾을 수 있다면

$product = $_GET['product']; 
    $city = $_GET['city']; 

    $query = "SELECT * FROM $product WHERE city = '$city'"; 
    $result = mysqli_query($con, $query) or die ("Could not connect to database."); 
    $product = str_replace('_', ' ', $product); 
    $product = strtoupper($product); 
     echo "You have searched for " . $product . " in " . $city;  

    if(mysqli_affected_rows($con) ==0){ echo "No records found"; } 

    else{ 
     echo "<table border=1>"; 
     echo "<tr> <th>Store</th> <th>City</th> </tr>"; 
     while ($row = mysqli_fetch_array($result)) { 
     echo "<tr><td>"; 
     echo $row['store']; 
     echo "</td><td>"; 
     echo $row['city']; 
     echo "</td></tr>"; 
     } 
     echo "</table>"; 
    } 
0

당신은이 코드를 시도해야합니다.

$result = mysqli_query($con, $query) or die(mysqli_error($con)); 

OR 
if(!$result){ 
echo die(mysqli_error($result)); 
} 
+0

의견을 남겨주셔서 감사합니다. 해결책을 찾았습니다. :) – user3627135

관련 문제