-1

我是 PHP 新手,这是一个学习过程。现在我已经厌倦了解决这个问题。我有一个 mysql 数据库,其中有几行退出。喜欢 :

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

当我运行查询以获取此数据库内容时,它不会显示重复的值内容。例如。在我的pro_no_sleep行 2 值是相同的:像1

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>";
    }

}
?>
4

1 回答 1

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) {
于 2012-12-08T04:45:53.070 回答