2012-12-08 109 views
-1

我是PHP新手,它是學習過程。現在我已經厭倦瞭解決這個問題。我有一個MySQL數據庫,其中有幾行退出。像:Mysql查詢不工作屬性

1) pro_no_sleep 2) pro_country 3) pro_state 4) pro_city etc.. 

當我運行查詢來獲取此數據庫內容時,它不顯示重複的值內容。例如。在我pro_no_sleep行2的值相同:像

pro_no_sleep 
1 
1 
2 
3 

所以,當我運行以下用PHP它不顯示覆制值中的內容查詢。它只顯示獨特的價值內容。像2和3

$search = mysql_query("SELECT * FROM property_step1 WHERE pro_no_sleep = '$people' OR 
pro_country = '$country' OR pro_state = '$region' OR pro_city = '$destination'"); 

我沒有在我的查詢中使用,因爲HTML表單表跡象已經從選擇標籤固定值。

PHP代碼:

<?php 
if (isset($_GET['Submit']) && $_GET['Submit'] == "Search") { 
    require_once("func.php"); 
    $people  = mysql_real_escape_string(htmlspecialchars(trim($_GET['people']))); 
    $country  = mysql_real_escape_string(htmlspecialchars(trim($_GET['country']))); 
    $region  = mysql_real_escape_string(htmlspecialchars(trim($_GET['region-depart']))); 
    $destination = mysql_real_escape_string(htmlspecialchars(trim($_GET['destination']))); 
    $from  = mysql_real_escape_string(htmlspecialchars(trim($_GET['from']))); 
    $to   = mysql_real_escape_string(htmlspecialchars(trim($_GET['to']))); 
    $upload  = "user/content/uploaded"; 


    $search = mysql_query("SELECT * FROM property_step1 WHERE pro_no_sleep = '$people' OR pro_country = '$country' OR pro_state = '$region' OR pro_city = '$destination'"); 

    $num = mysql_num_rows($search); 

    if ($num == 1) { 
     echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $num . " result found."; 
     while ($result = mysql_fetch_array($search)) { 
      echo "<div id='rightsearch'>"; 
      $propertyid  = (int) $result['propertyid']; 
      $getimg   = mysql_query("SELECT * FROM property_step3 WHERE propertyid = 
      '$propertyid' ORDER BY imgname LIMIT 1"); 
      $protitle_d  = $result['pro_title']; 
      $country_d  = $result['pro_country']; 
      $region_d  = $result['pro_state']; 
      $destination_d = $result['pro_city']; 
      $pro_des_d  = $result['pro_des']; 
      $pro_type_d  = $result['pro_type']; 
      $pro_no_bed_d = $result['pro_no_bed']; 
      $pro_no_bath_d = $result['pro_no_bath']; 
      $pro_no_sleep_d = $result['pro_no_sleep']; 
      $getreview  = mysql_query("SELECT rating FROM property_review WHERE p_id = '$propertyid'"); 
      $num_review  = mysql_num_rows($getreview); 


      while ($reimg = mysql_fetch_array($getimg)) { 
       $img = $reimg['imgname']; 
       echo "<a href='details.php?propertyid=$propertyid'>"; 
       echo '<img src="' . $upload . '/' . $img . '" width="' . 100 . '" height=" ' . 100 . '" />'; 
       echo "</a>"; 
      } 

      echo "<h2>$protitle_d</h2>"; 
      echo "<p>$country_d, $region_d, $destination_d</p>"; 
      //echo myTruncate($pro_des_d, 100) . "<br/>";      
      echo $pro_des_d . "<br/>"; 
      echo "<h5>$pro_type_d, $pro_no_bed_d bedroom"; 
      if ($pro_no_bed_d > 1) { 
       echo $plural = "s"; 
      } 
      echo ","; 
      echo " $pro_no_bath_d bath"; 
      if ($pro_no_bath_d > 1) { 
       echo $plural2 = "s"; 
      } 
      echo ","; 
      echo " $pro_no_sleep_d sleep"; 
      if ($pro_no_sleep_d > 1) { 
       echo $plural3 = "s"; 
      } 
      echo "</h5>"; 
      echo "$num_review review"; 
      echo "</div>"; 
     } // first while statement 
    } // second if statment 
    else { 
     echo "<div id='rightsearch'>"; 
     echo "No result found"; 
     echo "</div>"; 
    } 

} 
?> 
+0

你如何迭代查詢結果來顯示數據?你是否在phpMyAdmin或其他MySQL客戶端運行查詢,並在那裏查看結果? – sachleen

+0

是的,我做到了。它不顯示重複價值。 – Babu

+0

我認爲這是我的查詢問題..我不知道。 – Babu

回答

0

這是你的問題:

$num = mysql_num_rows($search); 

    if ($num == 1) { 
     echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $num . " result found."; 
     while ($result = mysql_fetch_array($search)) { 
... 

mysql_num_rows返回結果的行數。所以你檢查結果是否只包含一行,迭代所有行(記住,只有一行!)並執行一些操作。

你需要做的是檢查結果是否包含至少一行。

if ($num >= 1) { 
+0

謝謝@sachleen。 – Babu