English 中文(简体)
利用PHP在MySQL搜索多个栏目
原标题:Search multiple columns in MySQL using PHP

我以多种不同方式搜索因特网,但找不到对我问题的答复。 我正在教授自己的网站开发,并试图建立一个在线房地产网站。

我在试图寻找财产时就产生了问题。 例如,我想搜索特定地区或邮政编码的所有财产。 当我试图搜索多个领域时,如第1行,处理第2行,密码和面积(如伦敦SW1V)。

下面是目前使用的问答式一米,希望改变它,以便我需要:

SELECT property.propertyid, property.propertyname
FROM   property
WHERE  property.address_1 LIKE  %txtSearchField% 
    OR property.address_2 LIKE  %txtSearchField% 
    OR property.postcode  LIKE  %txtSearchField% 
    OR property.area      LIKE  %txtSearchField% 
最佳回答

在座标中,构造你执行,无论什么是<条码>txtSearch FieldQ/code>(即使是空的)。 你也想把美元值放在这一变量的前面:<代码>txtSearch Field,这就是为什么你不能从你的投入形式获得任何结果,因为你总是寻求文本<代码>txtSearch Field,而不是变数<代码>txtSearch Field的内容。 (我猜测你使用一种以“超文本输入”为标题的输入表,即<代码>txtSearch Field)。 不要将你的超文本形式作为“职位”的方法,因为如果你不这样做,违约是“目标”。

If I`m right, you should rework your code in this way:

    <?php
    //Sanitize user input
    $txtSearchField = filter_var($_POST[ txtSearchField ], FILTER_SANITIZE_STRING);
    //SQL query
    $query = "SELECT property.propertyid, property.propertyname
    FROM   property
    WHERE  CONCAT(property.address_1,    ,
                  property.address_2,    ,
                  property.postcode,    ,
                  property.area) LIKE  %$txtSearchField% " //see the $ sign here
//Finally, execute query and get result
$result = mysql_query ($query) or die ( SQL error occured:  .mysql_error());
while ($array = mysql_fetch_assoc($result)) {
  echo $result[ address_1 ]. / .$result[ address_2 ]. / .$result[ postcode ]. / .$result[ area ]. <br/> ;
}
    ?>
问题回答

其效率不高,但对于低交通页而言最容易和最有利:

SELECT property.propertyid, property.propertyname
FROM   property
WHERE  CONCAT(property.address_1,    ,
              property.address_2,    ,
              property.postcode,    ,
              property.area) LIKE  %txtSearchField% 

这应当为你工作。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签