简体   繁体   English

从数组和变量过滤时,PHP mysql查询问题

[英]PHP mysql query issue when filtering from array and variables

On my webpage (html, php), I'm trying to get it such that users may select multiple checkboxes, each checkbox effectively filters what results the users see. 在我的网页(html,php)上,我试图让用户选择多个复选框,每个复选框有效地过滤了用户看到的结果。 Info is pulled from the database (MySQL) based on the values in different columns. 根据不同列中的值从数据库(MySQL)中提取信息。 As shown below, one column is Joint_1, another column is Position. 如下所示,一列是Joint_1,另一列是Position。

Effective code that WORKS for filtering (very static, not practical to use obviously) is this: WORKS用于过滤的有效代码(非常静态,显然不实用)是这样的:

$sql = "SELECT * FROM `Table_Name` WHERE (Joint_1=\'region1\'  OR  
                                          Joint_1=\'region2\'  OR  
                                          Joint_1=\'region3\' OR
                                          Joint_1=\'region4\') AND
                                         (Position=\'position1\' OR
                                          Position=\'position2\' OR
                                          Position=\'position3\')";
$result = $conn->query($sql);

if($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {
      echo $row["Common_Name1"] . "<br>";
   }
} else {
  echo "0 results";
  }

Below code is attempts at above code, but using arrays, which does NOT work. 下面的代码是对上面的代码的尝试,但是使用了数组,这是行不通的。

$regions = 
array('region1', 'region2', 'region3', 'region4');
$position = array('position1', 'position2', 'position3');

$sql = "SELECT * FROM `Table_Name` WHERE (Joint_1=\'. $regions[0] .\'  OR  
                                          Joint_1=\'. $regions[1] .\'  OR  
                                          Joint_1=\'. $regions[2] .\' OR
                                          Joint_1=\'. $regions[3] .\') AND
                                         (Position=\'. $position[0] .\' OR
                          Position=\'. $position[0] .\' OR
                      Position=\'. $position[0] .\')"; 

Above code provides results of '0 results.' 上面的代码提供的结果为“ 0结果”。

I've attempted to perform this numerous times, with additional NON-FUNCTIONAL CODE also below (below attempting to filter based on only 1 column as I have obviously not mastered the code to approach filtering based on 2 columns). 我已尝试执行多次,并在下面添加了其他非功能性代码(以下尝试仅基于1列进行过滤,因为我显然没有掌握基于2列进行过滤的代码)。

$sqlregion = array();
foreach ($_POST['region'] as $reg) {
   $sqlreg[] = "'" . mysql_real_escape_string($reg) . "'";   
}
$sql = "SELECT * FROM 'Exercises' WHERE Joint_1 IN (" . implode(",", 
$sqlreg) . ");";

$result=$conn->query($sql);

if($result->num_rows > 0) {
   while($row = $result->fetch_assoc()) {
     echo $row["Common_Name1"] . "<br>";
   } 
}

Any help is appreciated! 任何帮助表示赞赏! Thanks. 谢谢。

You can construct this query from arrays, you can use the IN clause in mysql to specify multiple OR values 您可以从数组构造此查询,可以在mysql中使用IN子句来指定多个OR

$regions = array('region1', 'region2', 'region3', 'region4');
$position = array('position1', 'position2', 'position3');
$regions = implode(",", $regions); // Converts array to string with comma as a separator
$position = implode(",", $position);
$sql = "SELECT * FROM `Table_Name` WHERE Joint_1 IN ($regions) AND Position IN ($position)";
echo $sql;

This gives you a query like this 这给你这样的查询

SELECT * FROM Table_Name WHERE Joint_1 IN (region1,region2,region3,region4) AND Position IN (position1,position2,position3)

add in your own LIMIT GROUP BY or ORDER BY parameters to suit your needs 添加您自己的LIMIT GROUP BYORDER BY参数以满足您的需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM