English 中文(简体)
• 如何建立一个动态的神秘问题,以适应所有用户
原标题:How to build a dynamic mysql query to suit all users

我需要你帮助我的网站搜索功能。 I m 开发一个成员区,用户可以根据某些标准或标准组合查询其他登记用户。

我现在的问题是,如何建立一个充满活力的神秘问题,以适应各种搜索标准的需要,因为标准数目各不相同。

通常,我可以采用事先确定的一套标准来撰写。

   WHERE param1 =  $param1 
   AND param2 =  $param2 
   AND param3 =  $param3 

How do I solve this problem?

最佳回答

如果问题在于你不知道用户将选择哪一种标准,但希望将结果退回到“空白”标准上,那么你可以采取以下措施:

$criteria_1 = $_POST[ criteria_1 ];
$criteria_2 = $_POST[ criteria_2 ];
$criteria_3 = $_POST[ criteria_3 ];

if(!$criteria_1 && !$criteria_2 && !$criteria_1) {

     echo "You must select at least one criteria!";

} else {

   // Run query mentioned below and return results.       

} 

接下来,他会问:

SELECT * from mytable
WHERE
(criteria1 =  $criteria_1  OR  $criteria_1  =   ) AND
(criteria2 =  $criteria_2  OR  $criteria_2  =   ) AND
(criteria3 =  $criteria_3  OR  $criteria_3  =   )

这将把任何空白(非选用)参数作为空白处理,忽视这些参数。 了解到,如果没有规定标准,它将恢复所有成果。

Another way to write the above is:

SELECT * from mytable
WHERE
criteria1 IN ( $criteria_1 ,   ) AND
criteria2 IN ( $criteria_2 ,   ) AND
criteria3 IN ( $criteria_3 ,   )

同样,允许完全不加入所有标准1 的结果。

问题回答

下面是你再次要求的例子:

$query = "SELECT * FROM mytable";

if ($_POST[ name ] == "Jack") {
   $query .= " WHERE name =  Jack ";
}
if ($_POST[ name ] == "Bob") {
   $query .= " WHERE name =  Bob ";
}

if ($_POST[ state ] != "") {
   $query .= " AND state =  " . mysql_real_escape_string($state) . " ";
}

//So now, in total, your query might look like this
//"SELECT * FROM mytable WHERE name =  Bob  AND state =  $state "    

$result = mysql_query($query);

页: 1 随附if的报表,在您对所有<代码>_POST变量进行抽查后即执行查询。

我看到了这样的问题,这样,如果你不想把某一栏的价值放在一栏中,你就把这栏放在了联合国利比里亚办事处:

SELECT *
FROM users
WHERE param1 = :param1
UNION
SELECT *
FROM users
WHERE param2 = :param2
UNION
SELECT *
FROM users
WHERE param3 = :param3

这假设,每栏中每一栏都有索引,你重新进行“蓝色”和搜索(并使用《设计书》)。

use your scripting language (php) to loop over the inputs...

这样就有一个结构:

WHERE 1=1

之后添加:

AND paramx =  $px  

目 录

$criteria = array();

//Populate your criteria and parameter arrays with input from the web page here
...
// $criteria should now have stuff in it

$sql = "SELECT * FROM mytable ";//Or whatever your sql query is
$count = 0;
foreach ($criteria as $key => $parameter) {
  if ($count == 0) {
    $sql = $sql."WHERE ".$key." = ".$parameter;
  } else {
    $sql = $sql."AND ".$key." = ".$parameter;
  }
  $count++;
}

尽管如此,这极易受到 s的注射攻击。 Try using

一种选择是,从营地/居民那里或与你合作的任何人那里打井,就像这一一样。

$param1 = (isset($searchParam1) ? "param1 = $searchParam2" : "1");
$param2 = (isset($searchParam2) ? "param2 = $searchParam2" : "1");
$param3 = (isset($searchParam3) ? "param3 = $searchParam3" : "1");

和问询一样。

www.un.org/Depts/DGACM/index_arabic.htm

would like to share this code to build dynamic mysql query with PHP Thx & regards

$vocabulary = (($page == "vocabulary") ? "image_name <>   " : ""); 
$groupcat = (($group != "") ? "group = $group" : ""); 

$var = array($vocabulary, $groupcat);   

$counter = "0";
$param = "";
for ($i=0;$i<count($var);$i++)
{
    if ($counter == "0" && $var[$i] != "" )     $param = "WHERE ";
    if ($counter > "0" && $var[$i] != "" ) $param = " AND ";

    if ($param  != "") 
    {
        $condition .= $param . $var[$i];
        $param="";
        $counter++;
    }
}   
echo "Condition : ". $condition;




相关问题
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 ...

热门标签