查询返回没有找到的行

时间:2012-06-19 15:09:54

标签: php mysql

我在PHP中开发了用于搜索MySQL数据库的表单,但无论我如何尝试它仍然根本不检索任何结果。我在下面的代码中找不到任何语法错误或其他类似问题,

    <?php
    $txtkv=$_POST['txtkv'];
    $cbgen=$_POST['txtgenerator'];
    $cbinsulation=$_POST['txtinsulation'];
    $cbclass=$_POST['txtclass'];
    $cbairinlet=$_POST['txtairInlet'];
    $cbip=$_POST['txtIp'];

    // set database server access variables: 
    $host = "localhost"; 
    $user = "root"; 
    $pass = ""; 
    $db = "nordhavn";

    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
    mysql_select_db($db) or die ("Unable to select database!"); 

    $query="SELECT Scania.GensetType, Scania.EngineType, Scania.Engine60Hz, Scania.Alternator,NordhavnGenset.MaxKw,  NordhavnGenset.MaxKva, Scania.PriceEur
    FROM Scania 
    LEFT JOIN NordhavnGenset ON Scania.Alternator=NordhavnGenset.Alternator 
    LEFT JOIN Generator ON Generator.Alternator=Scania.Alternator
    LEFT JOIN Insulation ON Insulation.Insulation=NordhavnGenset.Insulation
    LEFT JOIN Klasse ON Klasse.Klasse=NordhavnGenset.Class
    LEFT JOIN AirInletFilter ON AirInletFilter.AirInletFilter=NordhavnGenset.AirInletFilter
    LEFT JOIN IP ON IP.IP=NordhavnGenset.Ip
    WHERE (NordhavnGenset.MaxKva='".$txtkv."')   and (Generator.Alternator='".$cbgen."')  and (Insulation.Insulation='".$cbinsulation."')  and (Klasse.Klasse='".$cbclass."')  and (AirInletFilter.AirInletFilter='".$cbairinlet."')  and (IP.IP='".$cbip."')";        
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    $i = 0;
    if (mysql_num_rows($result) > 0) { 
          echo "<table cellpadding=10 border=1>
                <tr>
                    <td>    Gense Type (Scania.GensetType)  </td>
                <td>    Engine type (Scania.Enginetype) </td>
                <td>    Engine 60Hz  (Scania.Engine60hz)</td>
                <td>    Alternator (Scania.Alternator and Genrator.Alternator)  </td>   <td>    Max Kw  (NordhavnGenset.MaxKw)  </td>
                    <td>    Max Kva  (NordhavnGenset.MaxKva)</td>
                    <td>    Price Euro (Scania.PriceEur)    </td>
                </tr>";    
          while(($row = mysql_fetch_row($result)) !== false) { 
                $i++;
                echo "<tr class=\"d".($i & 1)."\">"; 
                echo "<td>"."<center>".$row[1]."</center>"."</td>";
                echo "<td>"."<center>".$row[2]."</center>"."</td>";
                echo "<td>"."<center>".$row[3]."</center>"."</td>";
                echo "<td>"."<center>".$row[4]."</center>"."</td>"; 
                echo "<td>"."<center>".$row[5]."</center>"."</td>";
                echo "<td>"."<center>".$row[6]."</center>"."</td>";
                echo "<td>"."<center>".$row[7]."</center>"."</td>";
                echo "<td>"."<center>".$row[8]."</center>"."</td>"; 
                echo "<td>"."<center>".$row[9]."</center>"."</td>";
                echo "<td>"."<center>".$row[10]."</center>"."</td>";
                echo "<td>"."<center>".$row[11]."</center>"."</td>";
                echo "</tr>"; 
            } 
                echo "</table>"; 
        }else { 
            echo "No rows found!"; 
        }     
    mysql_free_result($result); 
    mysql_close($connection); 
    ?>

1 个答案:

答案 0 :(得分:0)

你期待什么结果,你似乎有很多条件。搜索表单应该为用户提供许多标准,但只有在用户提供的内容时才使用它们。

例如,不要在一个大查询中一次使用所有标准,请执行以下操作:

$criterias = array("1=1");
if(isset($_POST['txtkv'])){ $criterias[] = 'NordhavnGenset.MaxKva = "'.mysql_real_escape_string($_POST['txtkv']).'"'; }
if(isset($_POST['cbgen'])){ $criterias[] = 'Generator.Alternator = "'.mysql_real_escape_string($_POST['cbgen']).'"'; }
if(isset($_POST['cbinsulation'])){ $criterias[] = 'Insulation.Insulation = "'.mysql_real_escape_string($cbinsulation).'"'; }
if(isset($_POST['cbclass'])){ $criterias[] = 'Klasse.Klasse = "'.mysql_real_escape_string($cbclass).'"'; }
if(isset($_POST['cbairinlet'])){ $criterias[] = 'AirInletFilter.AirInletFilter = "'.mysql_real_escape_string($cbairinlet).'"'; }
if(isset($_POST['cbip'])){ $criterias[] = 'IP.IP = "'.mysql_real_escape_string($cbip).'"'; }

$query = "
SELECT 
    Scania.GensetType, Scania.EngineType, Scania.Engine60Hz, 
    Scania.Alternator,NordhavnGenset.MaxKw, NordhavnGenset.MaxKva, 
    Scania.PriceEur

FROM 
    Scania 
    LEFT JOIN NordhavnGenset ON Scania.Alternator = NordhavnGenset.Alternator 
    LEFT JOIN Generator ON Generator.Alternator = Scania.Alternator
    LEFT JOIN Insulation ON Insulation.Insulation = NordhavnGenset.Insulation
    LEFT JOIN Klasse ON Klasse.Klasse = NordhavnGenset.Class
    LEFT JOIN AirInletFilter ON AirInletFilter.AirInletFilter = NordhavnGenset.AirInletFilter
    LEFT JOIN IP ON IP.IP = NordhavnGenset.Ip

WHERE 
    ".implode(' AND ', $criterias);

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

这将创建一个只包含您需要的标准的完整查询。请注意,在顶部,我们创建标准数组,然后在我们去的时候填充所有标准。我们还需要时间来逃避它们,以确保我们在原始请求中没有看到的输入。

然后,使用IMPLODE,我们在AND之间整理所有项目以创建标准列表。 “1 = 1”用于防止空WHERE子句,如果没有提供标准,但在所有情况下1 = 1总是为真,所以它不会过滤任何东西。

我希望这会有所帮助,我希望这真的是你的问题......

祝你好运